round any number up to the nearest 5 in python

To round any number up to the nearest 5 in Python, you can use the ceil function from the math module. The ceil function returns the smallest integer which is greater than or equal to the input number. So, you can first divide the number by 5, then apply ceil, and finally multiply by 5 to get the rounded up value.

Here's the code:

main.py
import math

def round_up_to_5(x):
    return int(math.ceil(x/5.0)) * 5
72 chars
5 lines

You can call this function with the number you want to round up as the argument, for example:

main.py
>>> round_up_to_5(13)
15
>>> round_up_to_5(17)
20
>>> round_up_to_5(6)
10
74 chars
7 lines

This function should work for both positive and negative numbers.

gistlibby LogSnag