赞
踩
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控制点前面与后面的位数。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。