gpt4 book ai didi

python - 基本的 Python 数字

转载 作者:太空宇宙 更新时间:2023-11-04 06:44:38 25 4
gpt4 key购买 nike

为什么 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

关于python - 基本的 Python 数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1893094/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com