四舍五入到最接近的 500,Python

编程入门 行业动态 更新时间:2024-10-27 20:39:39
本文介绍了四舍五入到最接近的 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

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

发布评论

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

>www.elefans.com

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