python中round的用法_python中如何使用round函数

python中round的用法_python中如何使用round函数_第1张图片

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

Python 2.7.8 (default, Jun 18 2015, 18:54:19)

[GCC 4.9.1] on linux2

Type "help", "copyright", "credits" or "license" for more information.

>>> round(0.5)

1.0$ python3

Python 3.4.3 (default, Oct 14 2015, 20:28:29)

[GCC 4.8.4] on linux

Type "help", "copyright", "credits" or "license" for more information.

>>> round(0.5)

0

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

number,要四舍五入的数,digits是要小数点后保留的位数如果 digits 大于 0,则四舍五入到指定的小数位。

如果 digits 等于 0,则四舍五入到最接近的整数。

如果 digits 小于 0,则在小数点左侧进行四舍五入。

如果round函数只有参数number,等同于digits 等于 0。

你可能感兴趣的:(python中round的用法)