python round rounding?
迷茫
迷茫 2017-07-05 10:35:02
0
3
1203
>>> round(0.5)
0
>>> round(1.5)#为什么这样
2
迷茫
迷茫

业精于勤,荒于嬉;行成于思,毁于随。

reply all(3)
我想大声告诉你

In different Python versions, the value of the round function will appear in different situations

In Python2.7, the definition of round function is that if the input value is the same distance from both sides, then take the even side

~ python2
Python 2.7.13 (v2.7.13:a06454b1afa1, Dec 17 2016, 12:39:47) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> round(0.5)
1.0

In Python3, the round function is defined as rounding.

Python 3.6.0 (v3.6.0:41df79263a11, Dec 22 2016, 17:23:13) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> round(0.5)
0

The guy upstairs used ipython, which is based on python2, so the definition also follows Python2.

过去多啦不再A梦

round(number, ndigits=None)
refers to the number of decimal places to be retained, the default is None

In [5]: round(0.5)
Out[5]: 1.0

In [6]: round(0.5, 1)
Out[6]: 0.5
ringa_lee

Try round(4.5)

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template