Rounding up numbers in Python can be done using the built-in function math.ceil():
Using math.ceil() function
The math.ceil() function rounds a number up to the nearest integer:
import mathmath.ceil(number)
Example with x = 7.2
import mathx = 7.2print( math.ceil(x) )
Ouput
8
Example with x = 7.7
import mathx = 7.7print( math.ceil(x) )
Ouput
8
Example with x = 7.0
Note that
import mathx = 7.0print( math.ceil(x) )
Ouput
7
Without using math module
Round a number up without using math module:
x = 7.7if int(x) - x < 0:print( int(x) + 1 )else:print( int(x) )
returns
8
References
| Links | Site |
|---|---|
| math.ceil(x) | docs.python.org |
| round() | docs.python.org |
