Convert Fraction String to Decimal?

Since no one has mentioned it yet there is a quick and dirty solution: var decimal = eval(fraction); Which has the perks of correctly evaluating all sorts of mathematical strings. eval(“3/2”) // 1.5 eval(“6”) // 6 eval(“6.5/.5”) // 13, works with decimals (floats) eval(“12 + 3”) // 15, you can add subtract and multiply too … Read more

Is there a reliable way in JavaScript to obtain the number of decimal places of an arbitrary number?

Historical note: the comment thread below may refer to first and second implementations. I swapped the order in September 2017 since leading with a buggy implementation caused confusion. If you want something that maps “0.1e-100” to 101, then you can try something like function decimalPlaces(n) { // Make sure it is a number and use … Read more

How to convert a decimal number into fraction?

You have two options: Use float.as_integer_ratio(): >>> (0.25).as_integer_ratio() (1, 4) (as of Python 3.6, you can do the same with a decimal.Decimal() object.) Use the fractions.Fraction() type: >>> from fractions import Fraction >>> Fraction(0.25) Fraction(1, 4) The latter has a very helpful str() conversion: >>> str(Fraction(0.25)) ‘1/4’ >>> print Fraction(0.25) 1/4 Because floating point values … Read more