当前位置:   article > 正文

python round的用法_round(128.15,-2)

round(128.15,-2)
round ( number [, ndigits ] )

Return the floating point value number rounded to ndigits digits afterthe decimal point. If ndigits is omitted, it defaults to zero. The resultis a floating point number. Values are rounded to the closest multiple of10 to the power minus ndigits; if two multiples are equally close,rounding is done away from 0 (so, for example, round(0.5) is 1.0 andround(-0.5) is -1.0).

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 fractionscan’t be represented exactly as a float. See Floating Point Arithmetic: Issues and Limitations formore information.


上面是官网的说法,试了几个就懂了。

>>> round(1.333)
1.0
>>> round(23.454545,3)
23.455
>>> round(23.4545,-1)
20.0
>>> round(23.4545,-2)
0.0

这个比int的“四舍五入”更加精确还可以控制位数。

ndigits控制点前面与后面的位数。


声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/726461
推荐阅读
相关标签
  

闽ICP备14008679号