๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ ref_math_ceil.asp
Python math.ceil() Method
# Import math library import math # Round a number upward to its nearest integer print(math.ceil(1.4)) print(math.ceil(5.3)) print(math.ceil(-5.3)) print(math.ceil(22.6)) print(math.ceil(10.0)) Try it Yourself ยป
๐ŸŒ
STEMpedia
ai.thestempedia.com โ€บ home โ€บ python functions โ€บ math.ceil()
Learn Python math.ceil() Function | Python Programming Tutorial
June 27, 2023 - In Python, the math.ceil() function is used to return the ceiling value of a number, which means it is used to round a number up to the nearest integer that is greater than the number itself.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ floor-ceil-function-python
floor() and ceil() function Python - GeeksforGeeks
ceil(): Rounds a number up to the nearest integer. Example: ceil() of 3.3 will be 4. Note: Both functions require importing the math module: import math
Published ย  January 10, 2018
๐ŸŒ
Board Infinity
boardinfinity.com โ€บ blog โ€บ what-is-ceil-function-in-python
What is ceil() function in Python? | Board Infinity
June 22, 2023 - The function will return a single integer, which must be the lowest integer that is bigger than or equal to the supplied value. Let's look at how to round numbers up to the closest integer using Python's math.ceil() method.
๐ŸŒ
Tutorialspoint
tutorialspoint.com โ€บ python โ€บ number_ceil.htm
Python math.ceil() Method
The Python math.ceil() method is used to find the nearest greater integer of a numeric value. For example, the ceil value of the floating-point number 3.6 is 4. The process involved is almost similar to the estimation or rounding off technique.
๐ŸŒ
Scaler
scaler.com โ€บ home โ€บ topics โ€บ python math.ceil() method
Python math.ceil() Method - Scaler Topics
November 29, 2023 - Ceil is a function defined in Python's math module which takes a numeric input and returns the integer greater than or equal to the input number.
๐ŸŒ
Codecademy
codecademy.com โ€บ docs โ€บ python:numpy โ€บ math methods โ€บ .ceil()
Python:NumPy | Math Methods | .ceil() | Codecademy
June 13, 2025 - The .ceil() function returns a NumPy array with the smallest integers greater than or equal to each element in x, returned as floats.
๐ŸŒ
Replit
replit.com โ€บ home โ€บ discover โ€บ how to use the ceil() function in python
How to use the ceil() function in Python | Replit
We'll cover real-world applications and offer debugging advice to help you implement the function effectively in your projects. import math result = math.ceil(4.2) print(result)--OUTPUT--5 ยท Because the ceil function belongs to Python's standard math library, you must first import math to make it accessible.
Find elsewhere
๐ŸŒ
Squash
squash.io โ€บ python-ceiling-function-explained
Python Ceiling Function Explained
It helps ensure accuracy and precision in calculations where rounding up is necessary. In Python, the math.ceil() function provides a convenient way to apply the ceiling function to numbers.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-math-ceil-function
math.ceil() function - Python - GeeksforGeeks
November 17, 2025 - math.ceil() function returns the smallest integer greater than or equal to a given number. It always rounds a value upward to the nearest whole number.
๐ŸŒ
Tutorial Gateway
tutorialgateway.org โ€บ python-ceil
Python ceil Function
March 26, 2025 - The Python ceil function is used to return the smallest integer value, which is greater than or equal to the specified expression or a specific number.
Top answer
1 of 9
490

No, but you can use upside-down floor division:ยน

def ceildiv(a, b):
    return -(a // -b)

This works because Python's division operator does floor division (unlike in C, where integer division truncates the fractional part).

Here's a demonstration:

>>> from __future__ import division     # for Python 2.x compatibility
>>> import math
>>> def ceildiv(a, b):
...     return -(a // -b)
...
>>> b = 3
>>> for a in range(-7, 8):
...     q1 = math.ceil(a / b)   # a/b is float division
...     q2 = ceildiv(a, b)
...     print("%2d/%d %2d %2d" % (a, b, q1, q2))
...
-7/3 -2 -2
-6/3 -2 -2
-5/3 -1 -1
-4/3 -1 -1
-3/3 -1 -1
-2/3  0  0
-1/3  0  0
 0/3  0  0
 1/3  1  1
 2/3  1  1
 3/3  1  1
 4/3  2  2
 5/3  2  2
 6/3  2  2
 7/3  3  3

Why this instead of math.ceil?

math.ceil(a / b) can quietly produce incorrect results, because it introduces floating-point error. For example:

>>> from __future__ import division     # Python 2.x compat
>>> import math
>>> def ceildiv(a, b):
...     return -(a // -b)
...
>>> x = 2**64
>>> y = 2**48
>>> ceildiv(x, y)
65536
>>> ceildiv(x + 1, y)
65537                       # Correct
>>> math.ceil(x / y)
65536
>>> math.ceil((x + 1) / y)
65536                       # Incorrect!

In general, it's considered good practice to avoid floating-point arithmetic altogether unless you specifically need it. Floating-point math has several tricky edge cases, which tends to introduce bugs if you're not paying close attention. It can also be computationally expensive on small/low-power devices that do not have a hardware FPU.


ยนIn a previous version of this answer, ceildiv was implemented as return -(-a // b) but it was changed to return -(a // -b) after commenters reported that the latter performs slightly better in benchmarks. That makes sense, because the dividend (a) is typically larger than the divisor (b). Since Python uses arbitrary-precision arithmetic to perform these calculations, computing the unary negation -a would almost always involve equal-or-more work than computing -b.

2 of 9
83

Solution 1: Convert floor to ceiling with negation

def ceiling_division(n, d):
    return -(n // -d)

Reminiscent of the Penn & Teller levitation trick, this "turns the world upside down (with negation), uses plain floor division (where the ceiling and floor have been swapped), and then turns the world right-side up (with negation again)"

Solution 2: Let divmod() do the work

def ceiling_division(n, d):
    q, r = divmod(n, d)
    return q + bool(r)

The divmod() function gives (a // b, a % b) for integers (this may be less reliable with floats due to round-off error). The step with bool(r) adds one to the quotient whenever there is a non-zero remainder.

Solution 3: Adjust the numerator before the division

def ceiling_division(n, d):
    return (n + d - 1) // d

Translate the numerator upwards so that floor division rounds down to the intended ceiling. Note, this only works for integers.

Solution 4: Convert to floats to use math.ceil()

def ceiling_division(n, d):
    return math.ceil(n / d)

The math.ceil() code is easy to understand, but it converts from ints to floats and back. This isn't very fast and it may have rounding issues. Also, it relies on Python 3 semantics where "true division" produces a float and where the ceil() function returns an integer.

๐ŸŒ
Python Guides
pythonguides.com โ€บ ceil-function-in-python
How To Use The Ceil() Function In Python?
March 19, 2025 - The ceil function in Python is used to return the smallest integer greater than or equal to a given number. This is particularly useful in scenarios where you need to round up numbers to avoid underestimating values.
๐ŸŒ
NumPy
numpy.org โ€บ doc โ€บ 2.2 โ€บ reference โ€บ generated โ€บ numpy.ceil.html
numpy.ceil โ€” NumPy v2.2 Manual
numpy.ceil(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature]) = <ufunc 'ceil'>#
๐ŸŒ
Analytics Vidhya
analyticsvidhya.com โ€บ home โ€บ understanding floor and ceiling functions in python
Floor and Ceiling Functions in Python | Applications and Behaviour
June 20, 2023 - The smallest integer bigger than or equal to โŒˆxโŒ‰ is the result of the ceil function, denoted by ceil(x) or x. A given number is rounded to the next whole number. We will discuss the formula and implementation of the ceil function in Python.
๐ŸŒ
GoLinuxCloud
golinuxcloud.com โ€บ home โ€บ python โ€บ python ceil() function explained [easy examples]
Python ceil() function Explained [Easy Examples] | GoLinuxCloud
January 13, 2024 - The ceil() function, part of Pythonโ€™s math module, is used for rounding a number up to the nearest integer. This function is particularly useful when you need an integer greater than or equal to a given number.
๐ŸŒ
Programiz
programiz.com โ€บ python-programming โ€บ numpy โ€บ methods โ€บ ceil
NumPy ceil() (With Examples)
The ceil() function rounds up each element in an array to the nearest integer greater than or equal to each element. The ceil() function rounds up floating point element(s) in an array to the nearest integer greater than or equal to the array element. Example import numpy as np array1 = ...
๐ŸŒ
Note.nkmk.me
note.nkmk.me โ€บ home โ€บ python
Round Up/Down Decimals in Python: math.floor, math.ceil | note.nkmk.me
January 15, 2024 - This custom function uses abs() to get the absolute value, math.ceil() for rounding up, and math.copysign() to retain the original sign. The resulting float from math.copysign() is then converted to an integer using int(). Get the absolute value ...