python round 四舍五入?
問題描述
>>> round(0.5)0>>> round(1.5)#為什么這樣2
問題解答
回答1:在不同的Python版本中,round函數(shù)的取值會出現(xiàn)不同狀況
在Python2.7中,round函數(shù)的定義是如果輸入數(shù)值距離兩邊一樣遠,則取偶數(shù)的一邊
~ python2Python 2.7.13 (v2.7.13:a06454b1afa1, Dec 17 2016, 12:39:47) [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwinType 'help', 'copyright', 'credits' or 'license' for more information.>>> round(0.5)1.0
在Python3 中,round函數(shù)的定義是四舍五入。
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 darwinType 'help', 'copyright', 'credits' or 'license' for more information.>>> round(0.5)0
樓上那位是用了ipython,是基于python2的,所以定義也遵從Python2的。
回答2:round(number, ndigits=None)指要保留的小數(shù)位,默認為None
In [5]: round(0.5)Out[5]: 1.0In [6]: round(0.5, 1)Out[6]: 0.5回答3:
試下round(4.5)
相關文章:
1. Docker for Mac 創(chuàng)建的dnsmasq容器連不上/不工作的問題2. docker安裝后出現(xiàn)Cannot connect to the Docker daemon.3. Span標簽4. javascript - 請教空白文本節(jié)點的問題5. extra沒有加載出來6. css - 關于border-image7. mysql - 為什么where條件中or加索引不起作用?8. python - linux怎么在每天的凌晨2點執(zhí)行一次這個log.py文件9. mysql - php 如何網(wǎng)址中出現(xiàn)該頁標題?10. django進行數(shù)據(jù)庫的查詢
