all Python 3Numbers & mathfunction
float.as_integer_ratio
as a fraction
Return a pair (numerator, denominator) exactly representing the float as a ratio of integers.
Syntax
x.as_integer_ratio()
The ratio describes the number actually stored in memory, not the literal you typed: 0.1 comes back as a pair of huge integers rather than 1 and 10, since the denominator is always a power of two. If you want the fraction a human would write, build fractions.Fraction from a string instead of from the float. On infinities the method raises OverflowError, on nan a ValueError.
Examples
print((0.5).as_integer_ratio()) # → (1, 2)
print((0.25).as_integer_ratio()) # → (1, 4)
print((2.0).as_integer_ratio()) # → (2, 1)
See also
Open in the full glossarydocs.python.orgReport an inaccuracy