【问题标题】:function round() doesn't work as I expect [duplicate]函数 round() 不能按我预期的那样工作[重复]
【发布时间】:2015-05-12 19:44:08
【问题描述】:

我在这里尝试使用圆形功能。有时它从.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

【问题讨论】:

  • 我猜这是python3?因为它适用于 python2
  • 四舍五入到偶数是许多数学领域的标准,尤其是统计数据,因此它通常也是许多编程语言的标准。

标签: python python-3.x rounding


【解决方案1】:

来自docs

如果两个倍数相等,则向偶数选择舍入

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

【讨论】:

  • “只是四舍五入” 这种特殊的技术(在 *.5 时四舍五入为偶数)称为"banker's rounding"
猜你喜欢
  • 1970-01-01
  • 2021-01-16
  • 1970-01-01
  • 2013-08-27
  • 2013-02-27
  • 2014-03-16
  • 2013-01-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多