在python3中,round(1.0/2.0)得到的是1,而在python2中,round(1.0/2.0)得到的是0
$python
Python2.7.8(default,Jun182015,18:54:19)
[GCC4.9.1]onlinux2
Type"help","copyright","credits"or"license"formoreinformation.
>>>round(0.5)
1.0
$python3
Python3.4.3(default,Oct142015,20:28:29)
[GCC4.8.4]onlinux
Type"help","copyright","credits"or"license"formoreinformation.
>>>round(0.5)
0
使用的一般語法是round(number,digits)
number,要四舍五入的數(shù),digits是要小數(shù)點后保留的位數(shù)
如果digits大于0,則四舍五入到指定的小數(shù)位。
如果digits等于0,則四舍五入到最接近的整數(shù)。
如果digits小于0,則在小數(shù)點左側(cè)進行四舍五入。
如果round函數(shù)只有參數(shù)number,等同于digits等于0。
以上內(nèi)容為大家介紹了python培訓(xùn)之如何使用round函數(shù),希望對大家有所幫助,如果想要了解更多Python相關(guān)知識,請關(guān)注IT培訓(xùn)機構(gòu):千鋒教育。