python round 四舍五入?
問題描述
>>> round(0.5)0>>> round(1.5)#為什么這樣2
問題解答
回答1:在不同的Python版本中,round函數(shù)的取值會出現(xiàn)不同狀況
在Python2.7中,round函數(shù)的定義是如果輸入數(shù)值距離兩邊一樣遠(yuǎn),則取偶數(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ù)位,默認(rèn)為None
In [5]: round(0.5)Out[5]: 1.0In [6]: round(0.5, 1)Out[6]: 0.5回答3:
試下round(4.5)
相關(guān)文章:
1. HTML5不支持frameset一般怎么解決?2. javascript - Vue的計算屬性底層依賴原理是怎么樣?3. javascript - jquery怎么給select option一個點擊時觸發(fā)的事件,如圖 如果選擇自定義觸發(fā)一個時間?4. 網(wǎng)頁爬蟲 - python爬蟲用BeautifulSoup爬取<s>元素并寫入字典,但某些div下沒有這一元素,導(dǎo)致自動寫入下一條,如何解決?5. 百度地圖api - Android百度地圖SDK,MapView上層按鈕可見卻不可觸,怎么解決?6. mysql 獲取時間函數(shù)unix_timestamp 問題?7. html5 - canvas中的mousedrag事件,為什么鼠標(biāo)拖出canvas,然后再次移入canvas,drag事件還觸發(fā)8. 新入手layuiadmin,部署到tp中。想用php自已寫一個后臺管理系統(tǒng)。9. javascript - react 組件 使用super()報錯10. javascript - es6擴展運算符...的問題
