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. python - 請問這兩個地方是為什么呢?2. php - 生產環境下,給MySQL添加索引,修改表結構操作,如何才能讓線上業務不受影響?3. python2.7 - python 正則前瞻 后瞻 無法匹配到正確的內容4. mysql 獲取時間函數unix_timestamp 問題?5. java - Mybatis 數據庫多表關聯分頁的問題6. 急急急!!!求大神解答網站評論問題,有大神幫幫小弟嗎7. android - 安卓做前端,PHP做后臺服務器 有什么需要注意的?8. (python)關于如何做到按win+R再輸入文件文件名就可以運行?9. mysql - Sql union 操作10. javascript - 按鈕鏈接到另一個網址 怎么通過百度統計計算按鈕的點擊數量
