Skip to main content
 首页 » 编程设计

python之基本的 Python 数字

2025年05月04日14cloudgamer

为什么 0.1 + 0.1 + 0.1 - 0.3 计算为 5.5511151231257827e-17 在 Python 中?

请您参考如下方法:

因为 float 就是这样工作的。如果您想要精确的数字,请使用 decimal模块。如果你想使用 float ,你必须记住在显示它们时将它们四舍五入到特定的精度。

>>> print '%.2f' % (0.1+0.1+0.1-0.3,) 
0.00