【发布时间】:2020-01-25 21:42:19
【问题描述】:
在使用round() 函数时,我注意到我得到了两个不同的结果,这取决于我是没有明确选择要包含的小数位数还是选择数字为 0。
x = 4.1
print(round(x))
print(round(x, 0))
它打印以下内容:
4
4.0
有什么区别?
【问题讨论】:
-
两种情况下我都得到 4.0 (python 3.5)
-
@taras: 不,Python 3.5 中的
round(4.1)产生4,只有round(4.1, 0)产生4.0。做三次检查你的 Python 版本。如果必须,请在 Python 内部使用import sys; print(sys.version_info),因为您报告的行为特定于 Python 2。round()function documentation for Python 3 明确涵盖了这种情况:如果ndigits被省略或者是None,它返回最接近的整数到它的输入。. -
@MartijnPieters,感谢您指出这一点。显然,我使用 2.7 检查过它,以为我运行的是 3.5。
标签: python python-3.x rounding