Home  >  Q&A  >  body text

python round rounding?

>>> round(0.5)
0
>>> round(1.5)#为什么这样
2
迷茫迷茫2662 days ago1185

reply all(3)I'll reply

  • 我想大声告诉你

    我想大声告诉你2017-07-05 10:37:02

    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.

    reply
    0
  • 过去多啦不再A梦

    过去多啦不再A梦2017-07-05 10:37:02

    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

    reply
    0
  • ringa_lee

    ringa_lee2017-07-05 10:37:02

    Try round(4.5)

    reply
    0
  • Cancelreply