views:

1092

answers:

4

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.

Thanks, Boda Cydo.

+11  A: 

The ceil function:

import math
print math.ceil(4.2)
Steve
Thanks. Didn't expect it to be so easy. I almost threw my computer out the window :(
bodacydo
Don't do that. Sell it on Craigslist, instead.
Steve
@bodacydo: Always ask at SO before throwing your computer out the window :).
MAK
Thanks guys. Will ask on SO each time I want to throw it out. :)
bodacydo
+4  A: 

math.ceil.

PiotrLegnica
Thanks. Didn't expect it to be so easy. I almost threw my computer out the window :(
bodacydo
+1  A: 
>>> import math
>>> math.ceil(5.4)
6.0
KennyTM
+2  A: 

ceil(number)

it is actually a standard function in many languages.

Andrey
I didn't know. Thanks for letting me know!
bodacydo