如何将数字四舍五入到最接近的 1000?

编程入门 行业动态 更新时间:2024-10-13 14:28:23
本文介绍了如何将数字四舍五入到最接近的 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?

本文发布于:2023-11-07 23:28:21,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1567726.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何将   最接近   四舍五入   数字

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!