📜  Python|十进制 as_integer_ratio() 方法

📅  最后修改于: 2022-05-13 01:55:28.595000             🧑  作者: Mango

Python|十进制 as_integer_ratio() 方法

Decimal#as_integer_ratio() : as_integer_ratio()是一个 Decimal 类方法,它在移出系数的最右边数字直到只剩下前导数字后返回指数:作为一对 (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)