snippetpythonCriticalCanonical
How do you round UP a number?
Viewed 0 times
youroundhownumber
Problem
How does one round a number UP in Python?
I tried
and not 3, as I would like.
Then I tried
I tried
round(number) but it rounds the number down. Here is an example:round(2.3) = 2.0and not 3, as I would like.
Then I tried
int(number + .5) but it round the number down again! Example:int(2.3 + .5) = 2Solution
The math.ceil (ceiling) function returns the smallest integer higher or equal to
For Python 3:
For Python 2:
x.For Python 3:
import math
print(math.ceil(4.2))For Python 2:
import math
print(int(math.ceil(4.2)))Code Snippets
import math
print(math.ceil(4.2))import math
print(int(math.ceil(4.2)))Context
Stack Overflow Q#2356501, score: 1382
Revisions (0)
No revisions yet.