Q:

How do you round UP a number in Python?

This problem is killing me. How does one roundup a number UP in Python?
I tried round(number) but it round the number down. Example:
round(2.3) = 2.0 and not 3, what I would like

The I tried int(number + .5) but it round the number down again! Example:
int(2.3 + .5) = 2

Then I tried round(number + .5) but it won't work in edge cases. Example:
WAIT! THIS WORKED!

Please advise.

A:

The ceil (ceiling) function:
import math
print(math.ceil(4.2))

A:

I know this answer is for a question from a while back, but if you don't want to import math and you just want to round up, this works for me.
>>> int(21 / 5)
4
>>> int(21 / 5) + (21 % 5 > 0)
5

The first part becomes 4 and the second part evaluates to "True" if there is a remainder, which in addition True = 1; False = 0. So if there is no remainder, then it stays the same integer, but if there is a remainder it adds 1.

A:

Interesting Python 2.x issue to keep in mind:
>>> import math
>>> math.ceil(4500/1000)
4.0
>>> math.ceil(4500/1000.0)
5.0

The problem is that dividing two ints in python produces another int and that's truncated before the ceiling call. You have to make one value a float (or cast) to get a correct result.
In javascript, the exact same code produces a different result:
console.log(Math.ceil(4500/1000));
5

