gpt4 book ai didi

python - 为什么 round(5/2) 返回 2?

转载 作者:太空狗 更新时间:2023-10-30 02:03:32 25 4
gpt4 key购买 nike

使用 python 3.4.3,

round(5/2) # 2

它不应该返回 3 吗?

我尝试使用 python 2,它给了我正确的结果

round(5.0/2) # 3

如何实现正确的 float 舍入?

最佳答案

if two multiples are equally close, rounding is done toward the even choice (so, for example, both round(0.5) and round(-0.5) are 0, and round(1.5) is 2).

引用documentation对于 round 函数。希望这会有所帮助:)

附带说明一下,我建议您在遇到此类问题时始终阅读文档 (haha)

关于python - 为什么 round(5/2) 返回 2?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30868788/

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