Skip to content

Python Ceiling: Rounding Up (and Python Ceiling Division)

Python Ceiling Ceil Cover Image

Learn how to use Python ceiling, math.ceil(), to round numbers up in Python, to the nearest integer value. By reading this tutorial, you’ll learn what the differences between the Python ceiling function in Python 2 and Python 3. You’ll also learn how the function is different from simply calling the int function. Finally, you’ll learn how to use Python to calculate ceiling division.

Being able to round numbers up in Python has many practical use cases. In many cases, this makes numbers intuitively easier to work with. Because of its many use cases, being able to round numbers in the way that you want will make you a strong programmer.

Let’s dive in!

The Quick Answer: Python ceiling returns a Rounded Up Integer

Quick Answer - Python Ceiling ceil to Round Up
Using Python’s math.ceil() function to round up

What does it mean to round up?

Normally, when rounding numbers, you round a number to the closest integer. This means that, say, you’re given the number 7.3. Because 7.3 is only 0.3 away from 7, but is 0.7 away from 8, the number will round down to 7.

However, if you want to round a number up, you can use the Python math.ceil() function. When you round up a number you round the number to the closest integer that is greater than the number. Because of this, when you round 7.3 up, it returns 8.

Knowing how to round numbers up has many useful applications in building your own Python programs. In the following sections, you’ll learn all you need to know about the Python ceiling function, math.ceil().

Want to learn how to get a file’s extension in Python? This tutorial will teach you how to use the os and pathlib libraries to do just that!

Understanding Python Ceiling()

The ceil() function is part of the built-in math module and takes a single argument, a number. The function returns a single number, an integer, that is the smallest integer greater than or equal to the number passed into the argument.

Let’s see how we can use the Python ceiling function, math.ceil(), to round number up to their nearest integer.

We’ll begin by importing the math library:

import math

We could also simply import the function directly, rather than importing the entire module:

from math import ceil

For the purposes of this tutorial, however, we’ll import the entire library.

Let’s see how we can pass in a few numbers, both positive and negative, and see how the Python ceiling function works:

# Using math.ceiling() to Round Up in Python
import math

# Returns: 
# 8
# -7

The math.ceil() Python function rounds the number up to the smallest integer that is greater than or equal to the number passed into it. Because of this, a positive number rounds to the next number. A negative number, meanwhile, returns the truncated values of the number.

In the next section, you’ll learn how the Python ceiling function, math.ceil behaves differently between Python 2 and Python 3.

Want to learn how to use the Python zip() function to iterate over two lists? This tutorial teaches you exactly what the zip() function does and shows you some creative ways to use the function.

Difference Between Python Ceiling in Python 2 and Python 3

Between versions 2 and 3 of Python, the math library implemented a different way of handling the ceil() ceiling function.

Previously, in Python 2, the math.ceil() function would return a float value. Meanwhile, in Python 3, the math.ceil() function returns an integer value.

If you wanted to replicate the original Python 2 version of the ceiling function, you could simply convert the value to a float, as shown below:

# Replicate math.ceil from Python 2 in Python 3
import math

x = 3.2

# Returns: 4.0

In the next section, you’ll learn the how Python int() and Python math.ceil() behave differently.

Want to learn more about Python for-loops? Check out my in-depth tutorial that takes your from beginner to advanced for-loops user! Want to watch a video instead? Check out my YouTube tutorial here.

Differences Between Int and Ceiling in Python

The Python int() function takes a single input, typically a floating point value, and converts it to an integer. Because of this, the function can also be used to round value, but the behaviour is a bit different than expect.

Let’s see what happens when we pass a positive floating point value in both the int() and the math.ceil() functions in Python:

# Comparing int and math.ceil for positive floats
import math


# Returns:
# 7
# 8

We can see that, while the math.ceil() function rounds the value up, as expected, the int() function truncates anything beyond the decimal place.

However, it would be incorrect to say that the int() function rounds values down. When we look at negative floating point values, we notice that the behaviour changes:

# Comparing int and math.ceil for negative floats
import math


# Returns:
# -7
# -7

We can see that when working with negative floating point values, that the two functions return the same value. The two functions accomplish different things and, because of this, it’s important to understand the differences between these two functions.

Want to learn more about Python f-strings? Check out my in-depth tutorial, which includes a step-by-step video to master Python f-strings!

Python Ceiling Division

Python has a number of different ways to compute division operations, which you can learn about in my tutorial here, where the different methods are covered in great detail.

One of these operators is the Python floor division operator, //. When this operator is used, the floored integer value of the two values is returned.

One interesting piece about Python is that is there no built-in way to easily compare a ceiling division, meaning that the ceiling of the quotient of two values is returned.

We can emulate this, however, by developing our own function to accomplish this. Let’s see what this would look like:

# Developing a ceiling division function in Python
def ceiling_division(numerator, denominator):
    return -(-numerator // denominator)

print(ceiling_division(8, 3))

# Returns 3

Let’s break down what we’re doing in this function:

  1. We define our function and allow it to take two arguments, a numerator and a denominator
  2. We then return the negative value of the negative numerator floor divided by the denominator

The reason that this returns a ceiling value is that when a negative value is rounded down, it moves to the lower integer value (i.e., -7.3 would return -8). When we then turn that back into a negative number of itself, it becomes positive again.

Want to learn how to calculate and use the natural logarithm in Python. Check out my tutorial here, which will teach you everything you need to know about how to calculate it in Python.


In this tutorial, you learned all about how the Python ceiling function works. You learned what it means to round up, and how rounding up works differently in Python 2 and Python 3. You then learned the differences between calling the int function and calling the ceiling function in Python. Finally, you learned how to use Python to develop a way to create a ceiling division function, since Python only provides a floor division function.

To learn more about the Python ceiling function, check out the official documentation here.


1 thought on “Python Ceiling: Rounding Up (and Python Ceiling Division)”

  1. def main(text):
    text = (
    text.replace(‘|’, ”)
    .replace(‘\n’, ”)
    .replace(‘loc’, ”)
    .replace(‘ ‘, ”)

    str = text.split(‘,’)

    ret = {}

    for i in str:
    if i is not None and i != ”:
    item = i.split(‘:=’)
    ret[item[0]] = [i for i in list(item[1].split(‘;’)) if i != “”]

    return ret

Leave a Reply

Your email address will not be published.