Python|十进制 quantize() 方法
Decimal#quantize() : quantize()是一个 Decimal 类方法,它返回一个等于第一个十进制值(四舍五入)的值,该值具有第二个十进制值的指数。
Syntax: Decimal.quantize()
Parameter: Decimal values
Return: a value equal to first decimal value (rounded) having the exponent of second decimal value.
代码 #1:quantize() 方法的示例
# Python Program explaining
# quantize() 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.quantize() method
print ("\n\nDecimal a with quantize() method : ", a.quantize(b))
print ("Decimal b with quantize() method : ", b.quantize(b))
输出 :
Decimal value a : -1
Decimal value b : 0.142857
Decimal a with quantize() method : -1.000000
Decimal b with quantize() method : 0.142857
代码 #2:quantize() 方法的示例
# Python Program explaining
# quantize() 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.quantize() method
print ("\n\nDecimal a with quantize() method : ", a.quantize(b))
print ("Decimal b with quantize() method : ", b.quantize(b))
输出 :
Decimal value a : -3.14
Decimal value b : 3.21E+7
Decimal a with quantize() method : -0E+5
Decimal b with quantize() method : 3.21E+7