Python 中的 as_integer_ratio() 函数:获取给定有理数的简化分数
在本教程中,我们将编写一个程序,返回两个数字,它们的比率等于给定的浮点值。我们有一个名为 as_integer_ratio() 的方法可以帮助我们实现目标。
让我们看一些例子。
Input: 1.5 Output: 3 / 2 Input: 5.3 Output: 5967269506265907 / 1125899906842624
让我们检查一下代码。
示例
# 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]}')输出
运行上述代码后,您将获得以下结果。
3 / 2
让我们再看一个例子。
示例
# 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]}')输出
运行上述代码后,您将获得以下结果。
5967269506265907 / 1125899906842624
结论
如果您在本教程中有任何疑问,请在评论区提问。
广告
数据结构
网络
关系数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP