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