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

3
  • 1
    Something like round(8568/100)*100 or round(170/50)*50 ? Commented Jul 7, 2021 at 9:26
  • Is x always an integer? Which direction should halfway-cases round (e.g., 825 or 1350)? Commented Jul 7, 2021 at 14:27
  • Why would 170 round to 200 rather than 150? 150 is closer, so it's not clear to me what rule you're using here. Commented Jul 7, 2021 at 14:27

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.