本文介绍了如何将数字四舍五入到最接近的 1000?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在将数字列表向上和向下舍入到最接近的 1000 时遇到了一些问题.
下面是我的代码:
rev_list = []对于范围内的 i (12):rev = int(round(random.normalvariate(100000, 12000)))rev_list.append(rev)打印(rev_list)输出为:
[97277, 96494, 104541, 132060, 98179, 87862, 84718, 95391, 94674, 89773, 92790, 86122]我想将列表四舍五入到最接近的 1000.我该怎么做?
解决方案round 函数 可以将负 数字四舍五入到小数点的左边.例如:
>>>回合(15768,-3)16000>>>回合(1218,-3)1000所以简短的回答是:使用 -3 的第二个参数调用 round 以四舍五入到最接近的 1000.
I'm having some issue to round up and down of a list of number to the nearest 1000.
Below is my code:
rev_list = [] for i in range(12): rev = int(round(random.normalvariate(100000, 12000))) rev_list.append(rev) print(rev_list)The output is:
[97277, 96494, 104541, 132060, 98179, 87862, 84718, 95391, 94674, 89773, 92790, 86122]I would like to round the list to the nearest 1000. How can I do that?
解决方案The round function can take negative digits to round to, which causes it to round off to the left of the decimal. For example:
>>> round(15768, -3) 16000 >>> round(1218, -3) 1000So the short answer is: Call round with the second argument of -3 to round to the nearest 1000.
更多推荐
如何将数字四舍五入到最接近的 1000?
发布评论