在本教程中,我們將編寫一個程序,該程序返回兩個數字,它們的比率等于給定的float值。我們有一個稱為as_integer_ratio()的方法,可以幫助實現我們的目標。
讓我們看一些例子。Input:
1.5
Output:
3?/?2
Input:
5.3
Output:
5967269506265907?/?1125899906842624
讓我們檢查一下代碼。
示例#初始化浮點值
float_value?=?1.5
#使用?as?_?integer?_?ratio?()方法獲取整數元組
integers?=?float_value.as_integer_ratio()
#打印整數
print(f'{integers[0]}?/?{integers[1]}')
輸出結果
如果運行上面的代碼,您將得到以下結果。3?/?2
讓我們來看另一個例子。
示例#初始化浮點值
float_value?=?5.3
#使用as_integer_ratio()方法獲取整數元組
integers?=?float_value.as_integer_ratio()
#打印整數
print(f'{integers[0]}?/?{integers[1]}')
輸出結果
如果運行上面的代碼,您將得到以下結果。5967269506265907?/?1125899906842624