round函数的功能主要是对浮点数进行近似取值,保留几位小数。比如
>>> round(10.0/3, 2)
3.33
>>> round(20/7)
3
第一个参数是一个浮点数,第二个参数是保留的小数位数,可选,如果不写的话默认保留到整数。 但是实际使用过程的可能会导致一些舍入误差,且舍入误差与Python解释器的版本相关。
python2
$ python
Python 2.7.8 (default, Oct 25 2019, 15:51:11)
[GCC 4.9.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> round(0.5)
1.0
python3:
$ python3
Python 3.4.3 (default, Oct 25 2019, 15:51:11)
[GCC 4.8.4] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> round(0.5)
0