这在我看来非常简单 - 但为什么下面的两个 y 值不是 == 0?我认为 decimal 模块的全部意义在于去除浮尘……以下是将数字作为变量传递的数学例程的极其简化的版本。
from decimal import *
getcontext().prec = 2
q = Decimal(0.01)
x = Decimal(0.10) * Decimal(0.10)
y = Decimal(x) - Decimal(q)
print(x,y, Decimal(y))
'''
x== 0.010
y== -2.1E-19
Decimal(y) == -2.1E-19
'''
请您参考如下方法:
尝试将数字指定为字符串
>>> Decimal('0.10') * Decimal('0.10') - Decimal('0.0100')
>>> Decimal('0.000')
