python round 四舍五入?
問題描述
>>> round(0.5)0>>> round(1.5)#為什么這樣2
問題解答
回答1:在不同的Python版本中,round函數的取值會出現不同狀況
在Python2.7中,round函數的定義是如果輸入數值距離兩邊一樣遠,則取偶數的一邊
~ 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函數的定義是四舍五入。
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)指要保留的小數位,默認為None
In [5]: round(0.5)Out[5]: 1.0In [6]: round(0.5, 1)Out[6]: 0.5回答3:
試下round(4.5)
相關文章:
1. javascript - vue-router怎么不能實現跳轉呢2. npm鏡像站全新上線3. 老哥們求助啊4. css3 - 請問一下在移動端CSS布局布局中通常需要用到哪些元素,屬性?5. mySql排序,序號6. html5 - angularjs中外部模版加載無法使用7. django - 后臺返回的json數據經過Base64加密,獲取時用python如何解密~!8. node.js - node 客戶端socket一直報錯Error: read ECONNRESET,用php的socket沒問題哈。。9. tp6表單令牌10. 我的html頁面一提交,網頁便顯示出了我的php代碼,求問是什么原因?
