python中round用法

round 函数很简单，对浮点数进行近似取值，保留几位小数。比如

round(10.0/3, 2)
#结果：3.33
round(20/7)
#结果：3

1、round的结果跟python版本有关

python2和python3中有什么不同：

#python2
#Python 2.7.8 (default, Jun 18 2015, 18:54:19)
round(0.5)
#结果为：1.0
#python3
#Python 3.4.3 (default, Oct 14 2015, 20:28:29)
round(0.5)
#结果为：0

2、特殊数字round出来的结果可能未必是想要的。

round(2.675, 2)
#结果为：2.67

python2和python3的doc中都举了个相同的栗子，原文是这么说的：

Note

The behavior of round() for floats can be surprising: for example, round(2.675, 2)
gives 2.67 instead of the expected 2.68. This is not a bug: it’s a result of the fact
that most decimal fractions can’t be represented exactly as a float. See Floating
Point Arithmetic: Issues and Limitations for more information.