Convert decimal number to rational: Difference between revisions
Content deleted Content added
GP |
|||
Line 562:
=={{header|Python}}==
{{works with|Python|2.6+}}
Note that the decimal values of the task description are truncated in some cases.
The first loop limits the size of the denominator the second uses the Decimal module to convert an accurate Decimal representation of the given values to a fraction.
<lang python>>>> from fractions import Fraction
>>> for d in (0.9054054, 0.518518, 0.75): print(d, Fraction.from_float(d).limit_denominator(100))
0.9054054 67/74
Line 572 ⟶ 573:
0.75 3/4
>>> from decimal import Decimal
>>> for d in '0.9054054 0.518518 0.75'.split(): print(d, Fraction.from_decimal(Decimal(d)))
0.9054054 4527027/5000000
|