w3resource

Python Math: Convert a float to ratio


24. Float to Ratio Converter

Write a Python program to convert a float to ratio.

Sample Solution:

Python Code:

from fractions import Fraction
value = 4.2
print(Fraction(value).limit_denominator())

Sample Output:

21/5 

Flowchart:

Flowchart: Convert a float to ratio

For more Practice: Solve these Related Problems:

  • Write a Python program to convert a given float to its simplest fractional form and print the result as a ratio.
  • Write a Python function that takes a float as input and returns a string representing its ratio form using the fractions module.
  • Write a Python script to convert several float values to ratios and then display them in a tabular format.
  • Write a Python program to calculate the ratio of a float number by expressing it as a fraction in lowest terms and printing both numerator and denominator.

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a python program to find the next previous palindrome of a specified number.
Next: Write a Python program for nth Catalan Number.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.