Python最接近的数值数字

2024-03-28 09:02:46 发布

您现在位置:Python中文网/ 问答频道 /正文

我想将值四舍五入到最接近的10(0-4=0;5-14=1;15-24=2,等等) 我可以四舍五入到最接近的10,但我希望每个数字范围都是一位数,如示例所示。你知道吗

从-5到4=0,5到14=1,15到24=2等等。。。你知道吗

我所尝试的。。。你知道吗

int(math.ceil(x / 10.0)) * 10

它给出最接近的10,但从0开始,并给出最接近的10,而不是一位数。你知道吗

任何建议都是有用的。你知道吗


Tags: 示例数字math建议intceil
1条回答
网友
1楼 · 发布于 2024-03-28 09:02:46

我们可以简单地使用:

int(round(float(x)/10))

^{}

round(number[, ndigits])

Return the floating point value number rounded to ndigits digits after the decimal point. If ndigits is omitted, it defaults to zero. The result is a floating point number. Values are rounded to the closest multiple of 10 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 and round(-0.5) is -1.0).

相关问题 更多 >