

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
as_integer_ratio() in Python for reduced fraction of a given rational
In this tutorial, we are going to write a program that returns two numbers whose ratio is equal to the given float value. We have a method called as_integer_ratio() that helps to achieve our goal.
Let's see some examples.
Input: 1.5 Output: 3 / 2 Input: 5.3 Output: 5967269506265907 / 1125899906842624
Let's examine the code.
Example
# initializing the float value float_value = 1.5 # getting integers tuple using the as_integer_ratio() method integers = float_value.as_integer_ratio() # printing the integers print(f'{integers[0]} / {integers[1]}')
Output
If you run the above code, you will get the following results.
3 / 2
Let's see another example.
Example
# initializing the float value float_value = 5.3 # getting integers tuple using the as_integer_ratio() method integers = float_value.as_integer_ratio() # printing the integers print(f'{integers[0]} / {integers[1]}')
Output
If you run the above code, you will get the following results.
5967269506265907 / 1125899906842624
Conclusion
If you have any queries in the tutorial, ask them in the comment section.
- Related Questions & Answers
- Maximum rational number (or fraction) from an array in C++
- Decomposing rational number as a sum of rationals in JavaScript
- Fraction module in Python
- Python Rational numbers (fractions)
- Product of given N fractions in reduced form in C
- C++ Ratio of Mth and Nth Terms of an A. P. with Given Ratio of Sums
- Find Positive Integer Solution for a Given Equation in C++
- Converting a proper fraction to mixed fraction - JavaScript
- Find the smallest positive integer value that cannot be represented as sum of any subset of a given array in Python
- Check if the given array can be reduced to zeros with the given operation performed given number of times in Python
- Java Program to Print a Square Pattern for given integer
- Python program to print all distinct elements of a given integer array.
- Program to find common fraction between min and max using given constraint in Python
- Find LCM of rational number in C++
- Python program for permutation of a given string inbuilt function in python
Advertisements