Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

round up division python

# 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
 
PREVIOUS NEXT
Tagged: #division #python
ADD COMMENT
Topic
Name
1+9 =