How to round a number up to the nearest 500 in Python

1 Answer

0 votes
import math 
  
def roundUpToNearest500(num):
    return math.ceil(num / 500) * 500

print(roundUpToNearest500(3)) 

print(roundUpToNearest500(49)) 

print(roundUpToNearest500(550))  
print(roundUpToNearest500(450)) 
print(roundUpToNearest500(500))

print(roundUpToNearest500(751))

print(roundUpToNearest500(1300))

print(roundUpToNearest500(-1))
print(roundUpToNearest500(-10))
print(roundUpToNearest500(-700))





'''
run:

500
500
1000
500
500
1000
1500
0
0
-500

'''

 



answered Jul 18, 2022 by avibootz

Related questions

1 answer 105 views
1 answer 95 views
1 answer 123 views
1 answer 104 views
1 answer 100 views
1 answer 122 views
...