*Python Ceil is a rounding function that round decimals up to the nearest whole number. Python Floor rounds decimals down to the nearest whole number. Both ceil and floor are Python math functions. *

In programming, you may encounter a scenario where you want to round a number to the smallest integer greater or less than another number.

**Take this quiz to skip straight to final interviews with Remote Friendly Tech Companies!**

That’s where the Python `math.floor()`

and `math.ceil()`

methods come in. You can use the `math.floor()`

method to calculate and return the largest integer not greater than a certain number, and the `math.ceil()`

method to calculate the smallest integer not lesser than a certain number.

This tutorial will discuss how to use the `floor`

and `ceil`

methods to return the floor or ceiling of a provided value. We’ll also walk through examples of each of these methods in a program to showcase how they work.

## Python Floor

Calculating the floor of a number is a common mathematical function in Python. The `floor`

of a number refers to the nearest integer value which is less than or equal to the number. To put it another way, the floor of a number is the number rounded down to its nearest integer value.

The Python math module includes a method that can be used to calculate the floor of a number: `math.floor()`

. `math.floor()`

takes in one parameter, which is the number whose floor value you want to calculate.

Here’s the syntax for the `math.floor()`

method:

import math math.floor(number)

It’s worth noting that the `floor()`

function is part of the math library, so in order to use it, we first need to import the math library as we did above.

Let’s use an example to show how this method works. Say that we are working in a coffee shop and we want to create a calculator that rounds down the quantities of the beans we have available to their nearest whole number. This makes it easier for us to understand how much coffee we have left, which is helpful when placing new orders

We could round down the quantity of a bean to its nearest whole number using the following code:

import math quantity = 250.92 rounded = math.floor(quantity) print(rounded)

Our code returns the smallest integer closest to 250.92, which is: `250`

.

Let’s break down our code. On the first line, we import the math library. Then, we define a variable called `quantity`

which stores the quantity of the bean we have in storage. Then, we use the `math.floor()`

function to round down the `quantity`

variable to its nearest whole number. On the final line, we print out the floor of the `quantity`

variable.

In this case, the nearest whole number to 250.92 is 250, so our code returned 250.

Similarly, we can use the `math.floor()`

method on negative numbers. So, let’s say that we are writing a program that calculates how many beans we’ll have left at the end of the month. Our program has projected that, given how many sales we have seen so far, we will have a negative amount of beans. In other words, we will run out of beans.

Now, we want to round down our value to the nearest whole number so we know how many beans to order, based on current demand. We could do so using the following code:

import math quantity_projection = -25.21 rounded = math.floor(quantity_projection) print(rounded)

Our code returns: `-26`

. As you can see, our program has rounded down our negative value to the nearest whole integer, which in this case is -26.

## Python Ceil

The `math.ceil()`

method is the opposite of the `math.floor()`

method. Whereas `floor`

rounds down a number to its nearest whole value, `ceil`

rounds a number up to its nearest whole value, and returns the ceiling value.

Here’s the syntax for the `math.ceil()`

method:

import math math.ceil(number)

As you can see, the syntax for the ceil function is the same as the syntax for `math.floor()`

. Both methods take in one parameter: the number you want to process using the method.

Let’s discuss an example of the `math.ceil()`

method in action. Say that we have decided we want to calculate the ceiling value of each bean quantity. In other words, we want to know the smallest whole number above each bean quantity. We want to calculate this so we know how many beans to order in our next shipment.

We could use the following code to round up the bean quantity we have to the nearest whole number:

import math quantity = 22.15 rounded = math.ceil(quantity) print(rounded)

Our code returns: `23`

. The `math.ceil()`

function has rounded up our quantity to the nearest whole integer not less than the quantity, which in this case is 23.

Similarly, we can use `math.ceil()`

on negative numbers. Let’s use the example of the program we discussed earlier that projects how many beans we’ll have left in stock at the end of the month given demand. But this time, instead of finding the floor value of our remaining quantity, we want to find the ceiling value. We could do so using this program:

import math quantity_projection = -25.21 rounded = math.floor(quantity_projection) print(rounded)

Our code returns: `-26`

. Our program has rounded our quantity projection up to the nearest whole number, which in this case was -26.

## Python Floor and Ceil vs. Round

When you’re just getting started with `math.floor()`

and `math.ceil()`

, you may be asking: how do these methods differ from the `round()`

method?

The `round()`

method can be used in Python to round a number to the nearest integer. However, it is not exactly the same as the `floor()`

and `ceil()`

methods.

`Round()`

searches for the nearest number, which could include decimals, while `math.floor()`

and `ceil()`

round up and down to the nearest `integer()`

, respectively. So, if you wanted to round a number like 105.2529 to two decimal places, you’d want to use `round()`

instead of `floor()`

or `ceil()`

.

If you’re interested in learning more about how to use the `round()`

method, check out our tutorial on Python round.

## Conclusion

The `math.floor()`

method allows you round a number down to its nearest whole integer, while the `math.ceil()`

method allows you to round a number up to its nearest whole integer.

This tutorial discussed how to use both the `math.floor()`

and `math.ceil()`

functions to round numbers in Python. We also walked through an example of each of these methods in a program. Now you’re ready to start using `math.floor()`

and `math.ceil()`

like a Python pro!