0

I would like to write a specif rounding logic.

number = x

if x < 950:
    # round number to and in steps of 50
elif x < 9000:
    # round number to and in steps of 100
elif x < 100000:
    # round number to and in steps of 250
else:
    # round number to and in steps of 1000

This is my idea how it could work.

What I need for example, if x = 123 then the it should be rounded to 100. if x = 170 it should be rounded to 200, and then for example for x = 8568 it should round to 8600

exec85
  • 447
  • 1
  • 5
  • 21

1 Answers1

3

Mostlikely not the cleanest way to do it but : (x + step/2)//step*step should work.

Example : print((880+25)//50*50) returns 900.

Youenn FS
  • 76
  • 2