xxxxxxxxxx
import math
number = 3.7
rounded_up = math.ceil(number)
print(rounded_up)
xxxxxxxxxx
>>> import math
>>> math.ceil(5.2)
6
>>> math.ceil(5)
5
>>> math.ceil(-0.5)
0
xxxxxxxxxx
#math.floor(number)
import math
print(math.floor(3.319)) #Prints 3
print(math.floor(7.9998)) #Prints 7
xxxxxxxxxx
# no import needed
# take advantage of floor division by negating a and flooring it
# then negate result to get a positive / negative (depending on input a)
def round_up(a, b = 1):
return -(-a // b) # wrap in int() if only want integers
>>> round_up(3.2) # works on single digits
4.0
>>> round_up(5, 2)
3
>>> round_up(-10, 4) # works on negative numbers
-2