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 - 有適合開發手機端Html5網頁小游戲的前端框架嗎?2. javascript - arguments.callee3. javascript - js setTimeout在雙重for循環中如何使用?4. java - 線上應用,如果數據庫操作失敗的話應該如何處理?5. atom開始輸入!然后按tab只有空格出現沒有html格式出現6. node.js - 阿里云ECS,阿里云Docker,還有Leancloud的LeanEgine,哪個更適合NodeJs WebApp?7. java - 創建maven項目失敗了 求解決方法8. mac里的docker如何命令行開啟呢?9. mysql - 這種分級一對多,且分級不平衡的模型該怎么設計表?10. mysql - linux連接數據庫報錯
