Manual
Return the floating point value number rounded to ndigits digits after the decimal point. If ndigits is omitted or is None, it returns the nearest integer to its input. Delegates to number.round(ndigits).
For the built-in types supporting round(), values are rounded to the closest multiple of 10 to the power minus ndigits; if two multiples are equally close, rounding is done toward the even choice (so, for example, both round(0.5) and round(-0.5) are 0, and round(1.5) is 2). The return value is an integer if called with one argument, otherwise of the same type as number.
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.
直译
返回浮点数number取整(四舍五入)到小数点后第ndigits位。如果ndigits缺省或为None,它返回输入的相近整数。该函数代表number.round(ndigits)。
对于支持round()的内建类型,将它的值四舍五入到最接近的10的倍数,然后进行负ndigits次幂运算;如果两倍等价接近,则取整(四舍五入)结束,最终选择会偏向偶数(例如:round(0.5)和round(-0.5) 值为0,round(1.5)值为2)。如果只有一个参数,返回值是偶数,否则与number类型相同(意思是两个参数都有)。
实例
>>> round(0.5)
0
>>> round(1.5)
2
>>> round(2.5)
2
>>> round(3.5)
4
>>> round(4.5)
4
>>> round(3.1415926, 2)
3.14
Note
round在这里翻译成四舍五入还不够确切,如上实例所示,该函数只会遇偶数进位,奇数则舍去,需要在使用时注意。