python的round后面有多个0_python中round的用法

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。这不是一个错误:因为事实上大部分十进制小数不能用浮点数精确表示。更多信息,请参阅浮点数运算:问题和限制。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值