小数固定精度
Decimal fixed precision
我想在货币计算中使用小数,所以我想在逗号后正好处理两个数字。一开始我以为decimal的context中的prec
指的是属性,但是经过几次实验,我觉得有点糊涂了。
实验#1:
In [1]: import decimal
In [2]: decimal.getcontext().prec = 2
In [3]: a = decimal.Decimal('159.9')
In [4]: a
Out[4]: Decimal('159.9')
In [5]: b = decimal.Decimal('200')
In [6]: b
Out[6]: Decimal('200')
In [7]: b - a
Out[7]: Decimal('40')
实验#2:
In [8]: decimal.getcontext().prec = 4
In [9]: a = decimal.Decimal('159.9')
In [10]: a
Out[10]: Decimal('159.9')
In [11]: b = decimal.Decimal('200')
In [12]: b
Out[12]: Decimal('200')
In [13]: b - a
Out[13]: Decimal('40.1')
实验 #3:(prec
仍设置为 4)
In [14]: a = decimal.Decimal('159999.9')
In [15]: a
Out[15]: Decimal('159999.9')
In [16]: b = decimal.Decimal('200000')
In [17]: b
Out[17]: Decimal('200000')
In [18]: b - a
Out[18]: Decimal('4.000E+4')
为什么它像我的例子一样工作?我应该如何在我的(货币计算)案例中使用小数?
精度设置的是有效位数,不等于小数点后的位数
因此,如果精度为 2,则将有两位有效数字,因此像 40.1
这样具有 3 位有效数字的数字将减少到高两位有效数字,从而得到 40
.
用Decimal
设置小数点后的位数没有简单的方法。但是,您可以使用高精度并始终 round
您的结果保留两位小数:
>>> from decimal import Decimal, getcontext
>>> getcontext().prec = 60 # use a higher/lower one if needed
>>> Decimal('200') - Decimal('159.9')
Decimal('40.1')
>>> r = Decimal('200') - Decimal('159.9')
>>> round(r, 2)
Decimal('40.10')
decimal FAQ也包含一个类似的问答(使用quantize
):
Q. In a fixed-point application with two decimal places, some inputs have many places and need to be rounded. Others are not supposed to have excess digits and need to be validated. What methods should be used?
A. The quantize() method rounds to a fixed number of decimal places. If the Inexact trap is set, it is also useful for validation:
>>> TWOPLACES = Decimal(10) ** -2 # same as Decimal('0.01')
>>> # Round to two places
>>> Decimal('3.214').quantize(TWOPLACES)
Decimal('3.21')
>>> # Validate that a number does not exceed two places
>>> Decimal('3.21').quantize(TWOPLACES, context=Context(traps=[Inexact]))
Decimal('3.21')
>>> Decimal('3.214').quantize(TWOPLACES, context=Context(traps=[Inexact]))
Traceback (most recent call last):
...
Inexact: None
Q. Once I have valid two place inputs, how do I maintain that invariant throughout an application?
A. Some operations like addition, subtraction, and multiplication by an integer will automatically preserve fixed point. Others operations, like division and non-integer multiplication, will change the number of decimal places and need to be followed-up with a quantize() step:
>>> a = Decimal('102.72') # Initial fixed-point values
>>> b = Decimal('3.17')
>>> a + b # Addition preserves fixed-point
Decimal('105.89')
>>> a - b
Decimal('99.55')
>>> a * 42 # So does integer multiplication
Decimal('4314.24')
>>> (a * b).quantize(TWOPLACES) # Must quantize non-integer multiplication
Decimal('325.62')
>>> (b / a).quantize(TWOPLACES) # And quantize division
Decimal('0.03')
In developing fixed-point applications, it is convenient to define functions to handle the quantize() step:
>>> def mul(x, y, fp=TWOPLACES):
... return (x * y).quantize(fp)
>>> def div(x, y, fp=TWOPLACES):
... return (x / y).quantize(fp)
>>> mul(a, b) # Automatically preserve fixed-point
Decimal('325.62')
>>> div(b, a)
Decimal('0.03')
我发现的最佳方法是设置高值 prec
并使用 Decimal.quantize
来舍入结果:
decimal.getcontext().prec=100
a = Decimal('400000.123456789')
b = Decimal('200000.0')
a-b
>>> Decimal('2.0000E+5')
(a-b).quantize(Decimal('0.01'))
>>> Decimal('200000.12')
我想在货币计算中使用小数,所以我想在逗号后正好处理两个数字。一开始我以为decimal的context中的prec
指的是属性,但是经过几次实验,我觉得有点糊涂了。
实验#1:
In [1]: import decimal
In [2]: decimal.getcontext().prec = 2
In [3]: a = decimal.Decimal('159.9')
In [4]: a
Out[4]: Decimal('159.9')
In [5]: b = decimal.Decimal('200')
In [6]: b
Out[6]: Decimal('200')
In [7]: b - a
Out[7]: Decimal('40')
实验#2:
In [8]: decimal.getcontext().prec = 4
In [9]: a = decimal.Decimal('159.9')
In [10]: a
Out[10]: Decimal('159.9')
In [11]: b = decimal.Decimal('200')
In [12]: b
Out[12]: Decimal('200')
In [13]: b - a
Out[13]: Decimal('40.1')
实验 #3:(prec
仍设置为 4)
In [14]: a = decimal.Decimal('159999.9')
In [15]: a
Out[15]: Decimal('159999.9')
In [16]: b = decimal.Decimal('200000')
In [17]: b
Out[17]: Decimal('200000')
In [18]: b - a
Out[18]: Decimal('4.000E+4')
为什么它像我的例子一样工作?我应该如何在我的(货币计算)案例中使用小数?
精度设置的是有效位数,不等于小数点后的位数
因此,如果精度为 2,则将有两位有效数字,因此像 40.1
这样具有 3 位有效数字的数字将减少到高两位有效数字,从而得到 40
.
用Decimal
设置小数点后的位数没有简单的方法。但是,您可以使用高精度并始终 round
您的结果保留两位小数:
>>> from decimal import Decimal, getcontext
>>> getcontext().prec = 60 # use a higher/lower one if needed
>>> Decimal('200') - Decimal('159.9')
Decimal('40.1')
>>> r = Decimal('200') - Decimal('159.9')
>>> round(r, 2)
Decimal('40.10')
decimal FAQ也包含一个类似的问答(使用quantize
):
Q. In a fixed-point application with two decimal places, some inputs have many places and need to be rounded. Others are not supposed to have excess digits and need to be validated. What methods should be used?
A. The quantize() method rounds to a fixed number of decimal places. If the Inexact trap is set, it is also useful for validation:
>>> TWOPLACES = Decimal(10) ** -2 # same as Decimal('0.01') >>> # Round to two places >>> Decimal('3.214').quantize(TWOPLACES) Decimal('3.21') >>> # Validate that a number does not exceed two places >>> Decimal('3.21').quantize(TWOPLACES, context=Context(traps=[Inexact])) Decimal('3.21') >>> Decimal('3.214').quantize(TWOPLACES, context=Context(traps=[Inexact])) Traceback (most recent call last): ... Inexact: None
Q. Once I have valid two place inputs, how do I maintain that invariant throughout an application?
A. Some operations like addition, subtraction, and multiplication by an integer will automatically preserve fixed point. Others operations, like division and non-integer multiplication, will change the number of decimal places and need to be followed-up with a quantize() step:
>>> a = Decimal('102.72') # Initial fixed-point values >>> b = Decimal('3.17') >>> a + b # Addition preserves fixed-point Decimal('105.89') >>> a - b Decimal('99.55') >>> a * 42 # So does integer multiplication Decimal('4314.24') >>> (a * b).quantize(TWOPLACES) # Must quantize non-integer multiplication Decimal('325.62') >>> (b / a).quantize(TWOPLACES) # And quantize division Decimal('0.03')
In developing fixed-point applications, it is convenient to define functions to handle the quantize() step:
>>> def mul(x, y, fp=TWOPLACES): ... return (x * y).quantize(fp) >>> def div(x, y, fp=TWOPLACES): ... return (x / y).quantize(fp) >>> mul(a, b) # Automatically preserve fixed-point Decimal('325.62') >>> div(b, a) Decimal('0.03')
我发现的最佳方法是设置高值 prec
并使用 Decimal.quantize
来舍入结果:
decimal.getcontext().prec=100
a = Decimal('400000.123456789')
b = Decimal('200000.0')
a-b
>>> Decimal('2.0000E+5')
(a-b).quantize(Decimal('0.01'))
>>> Decimal('200000.12')