>>>round(0.5)0>>>round(1.5)#為什么這樣2
python round 四舍五入?
Qyouu
2019-05-25 18:01:59
TA貢獻(xiàn)1780條經(jīng)驗(yàn) 獲得超1個(gè)贊
在不同的Python版本中,round函數(shù)的取值會(huì)出現(xiàn)不同狀況在Python2.7中,round函數(shù)的定義是如果輸入數(shù)值距離兩邊一樣遠(yuǎn),則取偶數(shù)的一邊~python2Python2.7.13(v2.7.13:a06454b1afa1,Dec172016,12:39:47)[GCC4.2.1(AppleInc.build5666)(dot3)]ondarwinType"help","copyright","credits"or"license"formoreinformation.>>>round(0.5)1.0在Python3中,round函數(shù)的定義是四舍五入。Python3.6.0(v3.6.0:41df79263a11,Dec222016,17:23:13)[GCC4.2.1(AppleInc.build5666)(dot3)]ondarwinType"help","copyright","credits"or"license"formoreinformation.>>>round(0.5)0樓上那位是用了ipython,是基于python2的,所以定義也遵從Python2的。
TA貢獻(xiàn)1796條經(jīng)驗(yàn) 獲得超4個(gè)贊
round(number,ndigits=None)指要保留的小數(shù)位,默認(rèn)為NoneIn[5]:round(0.5)Out[5]:1.0In[6]:round(0.5,1)Out[6]:0.5
舉報(bào)