python - 函数 round() 没有像我预期的那样工作

标签 python python-3.x rounding

<分区>

我在这里尝试使用 round 函数。有时它从 .5 向下舍入,有时向上舍入。那有什么问题呢?

源文件:

print("rounding up 0.5 is",round(.5))
print("rounding up 1.5 is",round(1.5))
print("rounding up 2.5 is",round(2.5))
print("rounding up 3.5 is",round(3.5))

输出:

rounding up 0.5 is 0
rounding up 1.5 is 2
rounding up 2.5 is 2
rounding up 3.5 is 4

最佳答案

来自docs :

if two multiples are equally close, rounding is done toward the even choice

所以当你说四舍五入时,它不一定是四舍五入。这只是四舍五入。

关于python - 函数 round() 没有像我预期的那样工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30200081/

相关文章:

python - 在日志格式化程序中提取日志记录调用中的额外字段

python - Elasticsearch-Python-具有刷新功能的批量助手API

python - 从数组中选择一个随机元素

Python 3 异常删除封闭范围内的变量,原因不明

objective-c - 需要帮助舍入数字( float )

php - 四舍五入到小数点后第二位

excel - 在Excel中,如何四舍五入到最接近的斐波那契数

python - 在 os.walk() 中使用 os.remove() for 循环返回 FileNotFoundError

python - 当参数不变时,适合在 python 类中使用闭包吗?

python - 使用 python 中的 ruby​​ gem/命令