Pythonの丸め?
迷茫
迷茫 2017-07-05 10:35:02
0
3
1038
リーリー
迷茫
迷茫

业精于勤,荒于嬉;行成于思,毁于随。

全員に返信(3)
我想大声告诉你

在不同的Python版本中,round函数的取值会出现不同状况

在Python2.7中,round函数的定义是如果输入数值距离两边一样远,则取偶数的一边

~ python2
Python 2.7.13 (v2.7.13:a06454b1afa1, Dec 17 2016, 12:39:47) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "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 darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> round(0.5)
0

楼上那位是用了ipython,是基于python2的,所以定义也遵从Python2的。

いいねを押す +0
过去多啦不再A梦

round(number, ndigits=None)
指要保留的小数位,默认为None

In [5]: round(0.5)
Out[5]: 1.0

In [6]: round(0.5, 1)
Out[6]: 0.5
いいねを押す +0
ringa_lee

试下round(4.5)

いいねを押す +0
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!