How do you round UP a number in Python?

bodacydo picture bodacydo · Mar 1, 2010 · Viewed 848.2k times · Source

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.

Answer

Steve Tjoa picture Steve Tjoa · Mar 1, 2010

The ceil (ceiling) function:

import math
print(math.ceil(4.2))