PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, August 7, 2022

[FIXED] How to convert a decimal number into fraction?

 August 07, 2022     decimal, fractions, python     No comments   

Issue

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

Solution

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.



Answered By - Martijn Pieters
Answer Checked By - Willingham (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing