📜  Python|十进制 same_quantum() 方法

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

Python|十进制 same_quantum() 方法

Decimal#same_quantum() :same_quantum()是一个 Decimal 类方法,用于检查两个操作数是否具有相同的指数。

代码 #1:same_quantum() 方法的示例

# Python Program explaining 
# same_quantum() 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.same_quantum() method
print ("\n\nDecimal a with same_quantum() method : ", a.same_quantum(b))
  
print ("Decimal b with same_quantum() method : ", b.same_quantum(b))

输出 :

Decimal value a :  -1
Decimal value b :  0.142857


Decimal a with same_quantum() method :  False
Decimal b with same_quantum() method :  True

代码 #2:same_quantum() 方法的示例

# Python Program explaining 
# same_quantum() 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.same_quantum() method
print ("\n\nDecimal a with same_quantum() method : ", a.same_quantum(b))
  
print ("Decimal b with same_quantum() method : ", b.same_quantum(b))

输出 :

Decimal value a :  -3.14
Decimal value b :  3.21E+7


Decimal a with same_quantum() method :  False
Decimal b with same_quantum() method :  True