Round to the nearest 500, Python

后端 未结 3 1769
醉酒成梦
醉酒成梦 2021-02-02 12:54

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

3条回答
  •  清歌不尽
    2021-02-02 12:58

    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
    

提交回复
热议问题