python 划分精度

python division precision

我知道浮点数不准确,并且想知道当我在 Python 中执行以下操作时获取 0.08354 而不是 (0.08353999999999999) 的最佳方法:

d = 8.354/100

print (d)

使用内置 round() 函数:

>>> d = 8.354/100
>>> d
0.08353999999999999
>>> round(d, 6)
0.08354
>>> 

如果你想要绝对精度,使用Decimals:

>>> import decimal
>>> decimal.Decimal('8.354') / 10
Decimal('0.8354')