For your use case, use integer arithmetic. There is a simple technique for converting integer floor division into ceiling division:

items = 102
boxsize = 10
num_boxes = (items + boxsize - 1) // boxsize

Alternatively, use negation to convert floor division to ceiling division:

num_boxes = -(items // -boxsize)
Answer from Raymond Hettinger on Stack Overflow
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.

Discussions

Integer ceiling divide - Ideas - Discussions on Python.org
I would like python to have integer-ceiling-divide. The usual expression is -(x // -y), with similar semantics to // except rounding up instead of down. It is generally used to answer the question “I have x objects and containers of size y. How many containers do I need?” Because of this, ... More on discuss.python.org
🌐 discuss.python.org
1
May 8, 2025
Why is 11//2 = 5?
I understand // rounds the resulting number to the closest integer. This is untrue. // rounds down in Python. Here's an example. >>> 14 // 5 2 If the numbers are both positive, you can think of it just returning the integer part of the dividend quotient. That is, 14 / 5 is actually 2.8. So once you throw away the decimal part you're left with 2. (It's a little more complicated with negative numbers, though. Not much, but slightly.) More on reddit.com
🌐 r/learnpython
23
7
January 23, 2023
Why Python's Integer Division Floors
I point I found rather funny... In mathematical number theory, mathematicians always prefer the latter choice (floor). isn't right (it even says so in the linked Wikipedia article). Number Theorists tend use the method where a mod b gives the least nonnegative representative. More on reddit.com
🌐 r/ProgrammingLanguages
11
13
February 16, 2024
How to round up the answer of an integer division?
You could add 1 to the numerator before dividing ,i.e. (n+1)/2 In general, add (denominator - 1) More on reddit.com
🌐 r/C_Programming
26
33
May 26, 2020
🌐
GeeksforGeeks
geeksforgeeks.org › python › floor-ceil-function-python
floor() and ceil() function Python - GeeksforGeeks
Apart from using the math module, we can also compute the floor and ceil of a float using basic arithmetic operations like floor division (//) and addition.
Published   January 16, 2026
🌐
Wikipedia
en.wikipedia.org › wiki › HAL_Tejas
HAL Tejas - Wikipedia
1 week ago - Currently the Tejas is cleared to carry the Rafael Litening III targeting/reconnaissance pod, while an advanced version named Litening 4I will be integrated on the Tejas. The Litening 4I pod, developed by the C4I systems division of Rafael, enables the aircraft to carry out reconnaissance, surveillance and intelligence gathering, in addition to target acquisition.
🌐
Python.org
discuss.python.org › ideas
Integer ceiling divide - Ideas - Discussions on Python.org
May 8, 2025 - I would like python to have integer-ceiling-divide. The usual expression is -(x // -y), with similar semantics to // except rounding up instead of down. It is generally used to answer the question “I have x objects and containers of size y. ...
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-interview-questions
Top 50+ Python Interview Questions and Answers (2025) - GeeksforGeeks
October 14, 2025 - Also, The method ceil(x) in Pythonreturns a ceiling value of x i.e., the smallest integer greater than or equal to x. ... / represents precise division (result is a floating point number) whereas // represents floor division (result is an integer).
Find elsewhere
🌐
Mathspp
mathspp.com › blog › til › 001
TIL #001 – ceiling division in Python | mathspp
September 15, 2021 - This operator is equivalent to doing regular division and then flooring down: >>> # How many years in 10_000 days? >>> from math import floor; floor(10_000 / 365) 27 >>> 10_000 // 365 27 · Then, someone asked if Python also had a built-in for ceiling division, that is, an operator that divided the operands and then rounded up.
🌐
AskPython
askpython.com › home › is there a ceiling equivalent of // operator in python?
Is There a Ceiling Equivalent of // Operator in Python? - AskPython
May 25, 2023 - And instead of rounding off to the closest value it rounds off to the floor value. It is similar to / operator in C or C++. The ceiling division is dividing two numbers and then rounding it off to the ceiling value of the result.
🌐
Csc
aptraining.csc.com › python-ceiling
Mastering Python Ceiling: Efficient Rounding Techniques ...
June 8, 2025 - Empowering professionals with cutting-edge technical training and certification programs to drive innovation and career growth.
🌐
PKH Me
blog.pkh.me › p › 36-figuring-out-round,-floor-and-ceil-with-integer-division.html
Figuring out round, floor and ceil with integer division
November 25, 2022 - Python is following the round toward even choice rule. This is not what we are implementing here (Edit: a partial implementation is provided at the end though). There are many ways of rounding, so make sure you've clarified what method your language picked. The integer division is symmetrical around 0 but ceil and floor aren't, so we need a way get the sign in order to branch in one direction or another.
🌐
Python
docs.python.org › 3 › library › math.html
math — Mathematical functions
2 weeks ago - Return x with the fractional part removed, leaving the integer part. This rounds toward 0: trunc() is equivalent to floor() for positive x, and equivalent to ceil() for negative x.
🌐
Squash
squash.io › python-ceiling-function-explained
Python Ceiling Function Explained
September 4, 2024 - Related Article: How to Run External Programs in Python 3 with Subprocess · To use the ceiling function in Python, you need to call the math.ceil() method and pass in the number that you want to round up.
🌐
W3Schools
w3schools.com › python › ref_math_ceil.asp
Python math.ceil() Method
Python Examples Python Compiler Python Exercises Python Quiz Python Challenges Python Server Python Syllabus Python Study Plan Python Interview Q&A Python Bootcamp Python Certificate Python Training ... # 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 »
🌐
Medium
medium.com › @kevingxyz › the-art-of-pythons-ceiling-and-floor-notation-684d4d354e1e
The Art of Python’s Ceiling and Floor using Operator | by Kevin | Medium
August 8, 2020 - In Python, the floor function rounds the value towards negative infinity. By playing on this principle, you will realize that by taking the negative of negative floor, can do something like this: ... In the above code, you will be getting 4. ...
🌐
Python
bugs.python.org › issue46639
Issue 46639: Ceil division with math.ceildiv - Python tracker
February 4, 2022 - This issue tracker has been migrated to GitHub, and is currently read-only. For more information, see the GitHub FAQs in the Python's Developer Guide · This issue has been migrated to GitHub: https://github.com/python/cpython/issues/90797
🌐
Reddit
reddit.com › r/learnpython › why is 11//2 = 5?
r/learnpython on Reddit: Why is 11//2 = 5?
January 23, 2023 -

Fresh beginner here.

I understand // rounds the resulting number to the closest integer. I guess the type "rounding" is a different thing, but its said in the post linked below that Python 3 uses 'ties to even' rule. I'm wondering why 5.5 rounds down rather than up? Stuck in the theory, any clarification is appreciated - cheers!!

https://www.reddit.com/r/learnpython/comments/92ne2s/why_does_round05_0/?utm_source=share&utm_medium=web2x&context=3

🌐
JanBask Training
janbasktraining.com › community › python-python › is-there-a-ceiling-equivalent-of-operator-in-python
Is there a ceiling equivalent of // operator in Python? | JanBask Training Community
September 26, 2025 - This works because negating the numbers flips the floor division behavior, effectively turning it into ceiling division. ... The -(-a // b) trick is a clean and fast solution if you want to avoid importing modules. In summary, while Python doesn’t provide a direct ceiling operator like //, you can easily achieve it using either math.ceil() or the -(-a // b) method.
🌐
Python.org
discuss.python.org › ideas
Integer ceiling divide - #13 by madeleineth - Ideas - Discussions on Python.org
May 9, 2025 - math.div(a, b, integer=True, round='up') is verbose enough that the practice of writing wrappers would not end; my purpose in suggesting this is to eliminate duplicative, inconsistent wrappers. For completeness, the division variants I have personally seen in use, not counting specialized numerical libraries, are the one called / in python, the one called // in python, the integer-ceiling-division one we are discussing here, and “exact integer division”: def exact_integer_divide(x, y): q, ...
🌐
Python
bugs.python.org › issue43255
Issue 43255: Ceil division with /// operator - Python tracker
February 18, 2021 - This issue tracker has been migrated to GitHub, and is currently read-only. For more information, see the GitHub FAQs in the Python's Developer Guide · This issue has been migrated to GitHub: https://github.com/python/cpython/issues/87421
🌐
Board Infinity
boardinfinity.com › blog › what-is-ceil-function-in-python
What is ceil() function in Python? | Board Infinity
June 22, 2023 - The floor division operator in Python is one of them, and its symbol is /. The floored integer value derived from the two values is what is returned when this operator is used. Python's lack of an easy-to-use built-in feature to compare ceiling ...