If I get the number 46 and I want to round up to the nearest ten. How do can I do this in python?
46 goes to 50.
You can use math.ceil()
to round up, and then multiply by 10
Python 2
import math
def roundup(x):
return int(math.ceil(x / 10.0)) * 10
Python 3 (the only difference is you no longer need to cast the result as an int
)
import math
def roundup(x):
return math.ceil(x / 10.0) * 10
To use just do
>>roundup(45)
50