国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術文章
文章詳情頁

python round 四舍五入?

瀏覽:106日期:2022-06-26 16:29:50

問題描述

>>> round(0.5)0>>> round(1.5)#為什么這樣2

問題解答

回答1:

在不同的Python版本中,round函數(shù)的取值會出現(xiàn)不同狀況

在Python2.7中,round函數(shù)的定義是如果輸入數(shù)值距離兩邊一樣遠,則取偶數(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ù)位,默認為None

In [5]: round(0.5)Out[5]: 1.0In [6]: round(0.5, 1)Out[6]: 0.5回答3:

試下round(4.5)

標簽: Python 編程
相關文章:
主站蜘蛛池模板: 会昌县| 永清县| 商都县| 图木舒克市| 温宿县| 台州市| 嘉义市| 达拉特旗| 东阿县| 镇远县| 玉田县| 乳源| 沂南县| 博湖县| 宝山区| 大埔县| 蛟河市| 绵竹市| 西城区| 武义县| 偃师市| 项城市| 弋阳县| 福安市| 屯昌县| 巫山县| 华宁县| 工布江达县| 大安市| 元阳县| 惠来县| 济宁市| 城步| 舟山市| 潼南县| 页游| 康平县| 武夷山市| 扬州市| 炎陵县| 广州市|