Write a Python program to convert a given float value to ratio


The program converts a floating-point number into a fraction using the Fraction module from the fractions library in Python.

First, a floating-point number a is defined as 23.56. Then, the Fraction() constructor is used to convert the float into a fraction. limit_denominator() method is used to limit the denominator of the resulting fraction to a maximum value. If no argument is provided for limit_denominator(), it will use the default value of 1000000.

The resulting fraction is stored in the variable z. The program then prints the value of z which will be a fraction object that represents the closest rational approximation of the original floating-point number.

Source Code

from fractions import Fraction
a = 23.56
z = Fraction(a).limit_denominator()
print(z)

Output

589/25

Example Programs