python round 四舍五入?
問題描述
>>> round(0.5)0>>> round(1.5)#為什么這樣2
問題解答
回答1:在不同的Python版本中,round函數(shù)的取值會(huì)出現(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. 數(shù)組按鍵值封裝!2. java - web項(xiàng)目中,用戶登陸信息存儲(chǔ)在session中好 還是cookie中好,取決于什么?3. angular.js - webpack build后的angularjs路由跳轉(zhuǎn)問題4. mysql - 大部分?jǐn)?shù)據(jù)沒有行溢出的text字段是否需要拆表5. mysql federated引擎無法開啟6. mysql 新增用戶 主機(jī)名設(shè)定 失敗7. 單擊登錄按鈕無反應(yīng)8. ubuntu - mysql 連接問題9. mysql - 查詢字段做了索引為什么不起效,還有查詢一個(gè)月的時(shí)候數(shù)據(jù)都是全部出來的,如果分拆3次的話就沒問題,為什么呢。10. mysql儲(chǔ)存json錯(cuò)誤
