Round to the nearest 500, Python

后端 未结 3 1766
醉酒成梦
醉酒成梦 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:59

    I personally find rounding a but messy. I'd rather use:

    (x+250)//500*500
    

    // means integer division.

    EDIT: Oh, I missed that you round "up". Then maybe

    -(-x//500)*500
    

提交回复
热议问题