四舍五入到最接近的 500,Python [英] Round to the nearest 500, Python

查看:87
本文介绍了四舍五入到最接近的 500,Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种方法来四舍五入到最接近的 500.我一直在使用:

math.ceil(round(8334.00256 + 250, -3))

由此,我在 ArcGIS 中制作的地图的比例尺中获得了一个值.我有能力读取和写入比例因子(即 1:8334 ....基本上,你设置千分之一,它默认为一个比率)如果比例因子不是 500 的因子,我想四舍五入到下一个 500.math.ceil 将四舍五入任何十进制值,而 round(n,-3) 将四舍五入到最接近的千位,但我正在努力寻找一种方法来四舍五入到最接近的 500.

有什么建议吗?谢谢,迈克

解决方案

缩放、圆形、非缩放:

round(x/500.0) * 500.0

编辑:要向上取整到 500 的下一个倍数,请使用与 math.ceil() 相同的逻辑,而不是 round():

math.ceil(x/500.0) * 500.0

I'm looking to find a way to round up to the nearest 500.I've been using:

math.ceil(round(8334.00256 + 250, -3))

Whereby I have a value from a scale in a map I am making in ArcGIS. I have the ability to read and write the scale factor (i.e. 1:8334....basically, you set the thousandth and it defaults to a ratio) If the scale factor isn't a factor of 500, I want to round up to the next 500. The math.ceil will round up any decimal value, and the round(n,-3) will round to the nearest thousandth, but I'm struggling to find a way to round to the nearest 500.

Any suggestions? Thanks, Mike

解决方案

Scale, round, unscale:

round(x / 500.0) * 500.0

Edit: To round up to the next multiple of 500, use the same logic with math.ceil() instead of round():

math.ceil(x / 500.0) * 500.0

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

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