>> round(0.5)1.0$ python3Python 3.4.3 (default..._round(x,2)什么意思">
当前位置:   article > 正文

python中round(x、2)是什么意思,round方法python2.x与python3.x的差异

round(x,2)什么意思

$ python

Python 2.7.8 (default, Jun 18 2015, 18:54:19)

[GCC 4.9.1] on linux2

Type "help", "copyright", "credits" or "license" for more information.

>>> round(0.5)

1.0

$ python3

Python 3.4.3 (default, Oct 14 2015, 20:28:29)

[GCC 4.8.4] on linux

Type "help", "copyright", "credits" or "license" for more information.

>>> round(0.5)

0

如果我们阅读一下python的文档,里面是这么写的:

在python2.7的doc中,round()的最后写着,"Values are rounded to the closest multiple of 10 to the power minus ndigits; if two multiples are equally close, rounding is done away from 0." 保留值将保留到离上一位更近的一端(四舍六入),如果距离两端一样远,则保留到离0远的一边。所以round(0.5)会近似到1,而round(-0.5)会近似到-1。

但是到了python3.5的doc中,文档变成了"values are rounded to the closest multiple of 10 to the power minus ndigits; if two multiples are equally close, rounding is done toward the even choice." 如果距离两边一样远,会保留到偶数的一边。比如round(0.5)和round(-0.5)都会保留到0,而round(1.5)会保留到2。

所以如果有项目是从py2迁移到py3的,可要注意一下round的地方(当然,还要注意/和//,还有print,还有一些比较另类的库)。

>>> round(2.675, 2)

2.67

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/726394
推荐阅读
相关标签
  

闽ICP备14008679号