HOWTO · Python
How to Round Up a Number in Python
Learn how Python's ceiling functions and integer division round values and quotients up.
On this page
Rounding up means taking the ceiling: the least integer greater than or equal to a value. For example, the ceiling of 3.69 is 4, while the ceiling of 4.0 remains 4. This differs from rounding to the nearest integer, which can move a value either up or down. For a scalar number, use Python’s standard-library math.ceil(); for an integer quotient, integer division can avoid a floating-point intermediate. NumPy also provides numpy.ceil() for array-based calculations.
What It Means to Round Up in Python
The ceiling always moves toward positive infinity, not away from zero. Thus, math.ceil(-3.2) is -3, because -3 is the least integer that is still greater than or equal to -3.2. An exact integer stays unchanged. Keeping this definition in mind helps distinguish ceiling from round(), which selects a nearby value and follows different tie behavior.
Use math.ceil() to Round Up a Number
Import ceil from the built-in math module, or call it as math.ceil(). It accepts a real-valued number and returns a Python integer. In current Python, / produces a floating-point quotient even when both operands are integers, so applying math.ceil() to that quotient rounds it upward. For calculations where floating-point representation could affect the result, use exact inputs such as fractions.Fraction instead of first computing an inexact float.
Run this example:
import math
value = 3.69
print(math.ceil(value))
4
Round Up an Integer Quotient Without Floating-Point Division
When both operands are integers and the denominator is nonzero, -(-numerator // denominator) computes the ceiling of their quotient using floor division. It works for positive and negative operands, including a negative denominator. The // operator rounds toward negative infinity; negating before and after division turns that floor into a ceiling. This form keeps integer arithmetic exact for arbitrarily large Python integers.
For example, with 22 divided by 5, the quotient is 4.4, so the ceiling is 5. With -22 divided by 5, the quotient is -4.4, so the ceiling is -4. An exact multiple remains itself. The denominator must not be zero: division by zero raises ZeroDivisionError.
For -22 divided by 5, this example prints -4, the ceiling of -4.4.
numerator = -22
denominator = 5
quotient_ceiling = -(-numerator // denominator)
print(quotient_ceiling)
-4
This integer formula is for integer inputs. It does not replace math.ceil() for arbitrary real values. A shortcut such as (numerator + denominator - 1) // denominator also depends on the denominator being positive; avoid presenting that expression as a general signed-division formula.
Use numpy.ceil() for NumPy Values
For NumPy arrays or scalars, use the numpy.ceil() function. It applies the ceiling element by element. The floating-point array shown below produces floating-point results, even when every rounded value is mathematically integral. Convert to an integer array only when integer values are appropriate for the next operation; NumPy’s astype(int) truncates, so apply ceil first.
This optional example requires NumPy to be installed. numpy.ceil() rounds each element upward; converting afterward produces integer values. The outputs below show both the floating-point array and the converted integer array.
import numpy as np
values = np.array([2.1, 3.0, -2.1])
rounded = np.ceil(values)
print(rounded)
print(rounded.astype(int))
[ 3. 3. -2.]
[ 3 3 -2]
Choose an Approach and Check Its Limits
Use math.ceil() for a single built-in numeric value or a quotient whose inputs may be floats. Choose the integer-division identity when the numerator and nonzero denominator are integers and avoiding floating-point arithmetic matters. Choose numpy.ceil() when the values already belong to a NumPy array or scalar workflow; installing NumPy solely to round one ordinary number is unnecessary.
Check the input type and denominator before choosing a formula. Floating-point values can approximate decimal fractions, so a computed value that should be an exact integer may land just above or below it; ceil follows the actual represented value. For exact decimal or rational arithmetic, construct an exact value first and then apply an appropriate ceiling operation. A denominator of zero has no quotient, and both / and // raise ZeroDivisionError in that case. These distinctions make the standard-library function the clearest default and the integer formula a targeted optimization for exact integer quotients.