Because "round" will round a number such as 24 down to twenty, when I need the answer to be rounded up to 30. Please help me! I've been stuck on this for ages :(
Asked
Active
Viewed 7,519 times
2
-
1Please show us the function you actually made... will be easier to help you – Roberto Nov 30 '15 at 15:07
-
1Possible duplicate of [Python - round up to the nearest ten](http://stackoverflow.com/questions/26454649/python-round-up-to-the-nearest-ten) – Falko Nov 30 '15 at 15:11
-
6Use `x + (-x) % 10`. – Mark Dickinson Nov 30 '15 at 19:35
2 Answers
0
This function will round both up and down, correctly:
import math
def roundup(x, n=10):
res = math.ceil(x/n)*n
if (x%n < n/2)and (x%n>0):
res-=n
return res
num = [5,9,11,15]
r_num = [roundup(n) for n in num]
print(r_num)
# [10, 10, 10, 20]

Yaakov Bressler
- 9,056
- 2
- 45
- 69
-
-
-
-
Sure, but the question body is clear enough: the OP is quite explicit about wanting 24 to round to 30. – Mark Dickinson Jun 06 '19 at 18:13
-