Convert Fraction String to Decimal?

前端 未结 16 628
谎友^
谎友^ 2020-12-05 13:55

I\'m trying to create a javascript function that can take a fraction input string such as \'3/2\' and convert it to decimal—either as a string \'1.5\'

16条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-05 14:43

    It works with eval() method but you can use parseFloat method. I think it is better! Unfortunately it will work only with that kind of values - "12.2" not with "5/8", but since you can handle with calculation I think this is good approach!

提交回复
热议问题