如何将数字四舍五入到最接近的 1000? [英] How to round numbers to the nearest 1000?

查看:105
本文介绍了如何将数字四舍五入到最接近的 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)
1000

So the short answer is: Call round with the second argument of -3 to round to the nearest 1000.

这篇关于如何将数字四舍五入到最接近的 1000?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆