python标准库的解释及翻译
round(number[, ndigits])
Return the floating point value number rounded to ndigits digits after the decimal point. If ndigits is omitted, 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,将返回最接近输入的整数。底层调用的是number.__round__(ndigits)。
对于支持round()的内建类型,值舍入到10的最接近的负ndigits次幂的倍数;如果离两个倍数的距离相等,则舍入选择偶数(因此,round(0.5)和round(-0.5)都是0,而round(1.5)是2)。如果使用一个参数调用返回值是一个整数,否则其类型与number相同。
注意
浮点数round()的行为可能让人惊讶,例如round(2.675, 2)给出的是2.67而不是期望的2.68。这不是一个错误:因为事实上大部分十进制小数不能用浮点数精确表示。更多信息,请参阅浮点数运算:问题和限制。