首頁  >  問答  >  主體

python round 四捨五入?

雷雷
迷茫迷茫2662 天前1187

全部回覆(3)我來回復

  • 我想大声告诉你

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

    在不同的Python版本中,round函數的值會出現不同狀況

    在Python2.7中,round函數的定義是如果輸入數值距離兩邊一樣遠,則取偶數的一邊

    ~ 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

    在Python3 中,round函數的定義是四捨五入。

    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

    樓上那位是用了ipython,是基於python2的,所以定義也遵從Python2的。

    回覆
    0
  • 过去多啦不再A梦

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

    round(number, ndigits=None)
    指要保留的小數位,預設為None

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

    回覆
    0
  • ringa_lee

    ringa_lee2017-07-05 10:37:02

    試試round(4.5)

    回覆
    0
  • 取消回覆