Use the math.ceil() or numpy.ceil() method to round up floats in Python. The difference is that math.ceil() returns a integer and numpy.ceil() returns a float.
round up using math.ceil()
To use the ceil() method, first import the math module. Then we can round up the float value to the next integer.
# round up using math.ceil()
import math
myFloat = 1.11
rounded = math.ceil(myFloat)
print(rounded)
The float value is rounded up and an integer is returned.
2
round up using numpy.ceil()
To use the ceil() method in numpy we import the numpy module. Then we round up the float value.
# round up using numpy.ceil()
import numpy
myFloat = 1.11
rounded = numpy.ceil(myFloat)
print(rounded)
The float value is rounded up and float is returned.
2.0
Other rounding operations in Python
Python offers a variety of other rounding operations:
To round down use the int() function or the math.floor() method.
To round to nearest integer use the round() function or numpy.round().
To round to 2 decimals use the built-in round() function.
To round to 5 (or any other number) use a custom function.