Decimal#as_integer_ratio():as_integer_ratio()是一種十進製類方法,在移出係數的最右邊數字直到僅剩下前導數字之後,返回指數:成對(n,d)
用法:Decimal.as_integer_ratio()
參數:十進製值
返回:移出係數的最右邊的數字直到僅剩下前導數字後的指數:成對(n,d)
代碼1:as_integer_ratio()方法示例
# Python Program explaining
# as_integer_ratio() method
# loading decimal library
from decimal import *
# Initializing a decimal value
a = Decimal(-1)
b = Decimal('0.142857')
# printing Decimal values
print ("Decimal value a : ", a)
print ("Decimal value b : ", b)
# Using Decimal.as_integer_ratio() method
print ("\n\nDecimal a with as_integer_ratio() method : ", a.as_integer_ratio())
print ("Decimal b with as_integer_ratio() method : ", b.as_integer_ratio())
輸出:
Decimal value a : -1 Decimal value b : 0.142857 Decimal a with as_integer_ratio() method : (-1, 1) Decimal b with as_integer_ratio() method : (142857, 1000000)
代碼2:as_integer_ratio()方法的示例
# Python Program explaining
# as_integer_ratio() method
# loading decimal library
from decimal import *
# Initializing a decimal value
a = Decimal('-3.14')
b = Decimal('321e + 5')
# printing Decimal values
print ("Decimal value a : ", a)
print ("Decimal value b : ", b)
# Using Decimal.as_integer_ratio() method
print ("\n\nDecimal a with as_integer_ratio() method : ", a.as_integer_ratio())
print ("Decimal b with as_integer_ratio() method : ", b.as_integer_ratio())
輸出:
Decimal value a : -3.14 Decimal value b : 3.21E+7 Decimal a with as_integer_ratio() method : (-157, 50) Decimal b with as_integer_ratio() method : (32100000, 1)
相關用法
注:本文由純淨天空篩選整理自noobestars101大神的英文原創作品 Python | Decimal as_integer_ratio() method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。