Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert a decimal number into fraction?

I was wondering how to convert a decimal into a fraction in its lowest form in Python.

For example:

0.25  -> 1/4 0.5   -> 1/2 1.25  -> 5/4 3     -> 3/1 
like image 446
user2370460 Avatar asked Apr 28 '14 14:04

user2370460


People also ask

How do you convert 1.75 into a fraction?

Answer: 1.75 as a fraction is expressed as 7/4.

What is 2.5 as a fraction?

Answer: Fractional form of 2.5 is 5/2. For the removal of the decimal, we multiply and divide by 10. And we get, 2.5/1 x 10/10 = 25/10 = 5/2.

How do you turn 0.75 into a fraction?

Answer: 0.75 as a fraction is 3/4.

How do you turn 0.285 into a fraction?

Since there are no more common factors, the simplest form of the decimal 0.285 in fraction form is 57200. 57 200 .


2 Answers

You have two options:

  1. 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.)

  2. 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 can be imprecise, you can end up with 'weird' fractions; limit the denominator to 'simplify' the fraction somewhat, with Fraction.limit_denominator():

>>> Fraction(0.185) Fraction(3332663724254167, 18014398509481984) >>> Fraction(0.185).limit_denominator() Fraction(37, 200) 

If you are using Python 2.6 still, then Fraction() doesn't yet support passing in a float directly, but you can combine the two techniques above into:

Fraction(*0.25.as_integer_ratio()) 

Or you can just use the Fraction.from_float() class method:

Fraction.from_float(0.25) 

which essentially does the same thing, e.g. take the integer ratio tuple and pass that in as two separate arguments.

And a small demo with your sample values:

>>> for f in (0.25, 0.5, 1.25, 3.0): ...     print f.as_integer_ratio() ...     print repr(Fraction(f)), Fraction(f) ...  (1, 4) Fraction(1, 4) 1/4 (1, 2) Fraction(1, 2) 1/2 (5, 4) Fraction(5, 4) 5/4 (3, 1) Fraction(3, 1) 3 

Both the fractions module and the float.as_integer_ratio() method are new in Python 2.6.

like image 61
Martijn Pieters Avatar answered Oct 08 '22 10:10

Martijn Pieters


To expand upon Martijn Pieters excellent answer with an additional option due to the imprecision inherent with more complex floats. For example:

>>> f = 0.8857097 >>> f.as_integer_ratio() (1994440937439217, 2251799813685248)          # mathematically wrong >>> Fraction(f) Fraction(1994440937439217, 2251799813685248)  # same result but in a class >>> Fraction(f).limit_denominator() Fraction(871913, 984423)                      # still imprecise 

The mathematical result desired was 8857097/10000000 which can be achieved by casting to a string and then manipulating it.

Edited Response

I found a much simpler way to resolve the accuracy issue.

>>> Fraction(str(f)) Fraction(8857097, 10000000) 

Casting as to a string also allows for accurate Decimal instances

>>> Decimal(f).as_integer_ratio() (1994440937439217, 2251799813685248) >>> Decimal(str(f)).as_integer_ratio() (8857097, 10000000) 

Original Response

def float_to_ratio(flt):     if int(flt) == flt:        # to prevent 3.0 -> 30/10         return int(flt), 1     flt_str = str(flt)     flt_split = flt_str.split('.')     numerator = int(''.join(flt_split))     denominator = 10 ** len(flt_split[1])     return numerator, denominator 

Now let's test it:

>>> float_to_ratio(f) (8857097, 10000000)      # mathematically correct 

I will note that this kind of fraction precision is not optimized and will usually not be needed, but for completeness it is here. This function doesn't simplify the fraction, but you can do additional processing to reduce it:

>>> n = 0.5 >>> float_to_ratio(n) (5, 10) >>> Fraction(*float_to_ratio(n)) Fraction(1, 2) 
like image 23
Matt Eding Avatar answered Oct 08 '22 09:10

Matt Eding