Actually, you could still use the round function:
>>> print round(1123.456789, -1)
1120.0
This would round to the closest multiple of 10. To 100 would be -2 as the second argument and so forth.
Answer from data on Stack OverflowActually, you could still use the round function:
>>> print round(1123.456789, -1)
1120.0
This would round to the closest multiple of 10. To 100 would be -2 as the second argument and so forth.
round() can take ints and negative numbers for places, which round to the left of the decimal. The return value is still a float, but a simple cast fixes that:
>>> int(round(5678,-1))
5680
>>> int(round(5678,-2))
5700
>>> int(round(5678,-3))
6000
Videos
round does take negative ndigits parameter!
>>> round(46,-1)
50
may solve your case.
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
Hi, I'm wondering how I'd go about rounding any given integer to the nearest ten. (i.e, 11 into 10, 156 into 160, etc.)
EDIT: Got it, for anybody wondering the code I used was: x = floor(x)/10; x = x*10;