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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。