这篇文章给大家分享的是有关python中使用round函数的方法的内容。小编觉得挺实用的,因此分享给大家做个参考。一起跟随小编过来看看吧。

在python3中,round(1.0/2.0)得到的是1,而在python2中,round(1.0/2.0)得到的是0

$pythonPython2.7.8(default,Jun182015,18:54:19)[GCC4.9.1]onlinux2Type"help","copyright","credits"or"license"formoreinformation.>>>round(0.5)1.0

$python3Python3.4.3(default,Oct142015,20:28:29)[GCC4.8.4]onlinuxType"help","copyright","credits"or"license"formoreinformation.>>>round(0.5)0

使用的一般语法是round(number,digits)

number,要四舍五入的数,digits是要小数点后保留的位数

如果digits大于0,则四舍五入到指定的小数位。如果digits等于0,则四舍五入到最接近的整数。如果digits小于0,则在小数点左侧进行四舍五入。如果round函数只有参数number,等同于digits等于0。

返回的值是四舍五入后的值。

感谢各位的阅读!关于python中使用round函数的方法就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到吧!