Floor and Ceil Function in Python
# Floor and Ceil Function in Python
Python has two built-in functions which allow us to round a given number. These functions are the `floor` and `ceil` functions.
The `floor` function rounds a number down to the nearest integer, if the number is already an integer then it remains unchanged. For example, if we call `floor(3.7)`, the result will be 3.
The `ceil` function rounds a number up to the nearest integer, if the number is already an integer then it remains unchanged. For example, if we call `ceil(3.7)`, the result will be 4.
These functions are useful when we need to round a number to the nearest integer, and can be used in many applications. For example, if we are dealing with monetary values, we want to always round up or round down to the nearest integer. In this case, the `floor` and `ceil` functions can be used to round the values to the nearest integer.
Worried About Failing Tech Interviews?
Attend our webinar on
"How to nail your next tech interview" and learn
.png)
Hosted By
Ryan Valles
Founder, Interview Kickstart

Our tried & tested strategy for cracking interviews

How FAANG hiring process works

The 4 areas you must prepare for

How you can accelerate your learnings
Register for Webinar
## Floor Function
The floor function in Python is used to round a number down to the nearest integer. This function takes a single number as its argument and returns the largest integer that is smaller than or equal to the given number.
Example:
```
#Create a variable and set it equal to a decimal number
x = 2.7
# Call the floor function on the variable
y = math.floor(x)
#Print the result
print(y)
```
The output of the code above is 2.
## Ceil Function
The ceil function in Python is used to round a number up to the nearest integer. This function takes a single number as its argument and returns the smallest integer that is greater than or equal to the given number.
Example:
```
#Create a variable and set it equal to a decimal number
x = 2.3
# Call the ceil function on the variable
y = math.ceil(x)
#Print the result
print(y)
```
The output of the code above is 3.