Python

# Python floor() and ceil() functions

The floor() and ceil() functions are two useful functions of Python, and the math module is required to import for using these functions. Both functions are used to retrieve the round value of a decimal number. The floor() function takes a decimal number as an argument and returns an integer number less the argument value. The ceil() function takes a decimal number as an argument and returns an integer number higher than the argument value. The uses of these functions have been explained in this tutorial.

## Use of floor() function:

The syntax of this function is given below.

Syntax:
int floor(number)

The function takes a number and returns an integer that is less than or equal to the number.

## Example-1: Using floor() function for positive number

Create a python file with the following script to print the floor values of three different positive decimal numbers.

#Import math module
import math as mt

#Print the floor values of different positive decimal numbers
print ("The floor value of 300.50 is: ", mt.floor(300.50))
print ("The floor value of 300.56 is: ", mt.floor(300.56))
print ("The floor value of 300.68 is: ", mt.floor(300.68))

Output:
The following output will appear after executing the above script. ## Example-2: Using floor() function for negative number

Create a python file with the following script to print the floor values of two different negative decimal numbers.

#Import math module
import math as mt

#Print the floor values of different negative decimal numbers
print ("The floor value of -450.40 is: ", mt.floor(-450.45))
print ("The floor value of -450.70 is: ", mt.floor(-450.70))

Output:
The following output will appear after executing the above script. ## Example-3: Using floor() function for a mathematical expression

Any mathematical expression can be used as the argument of the floor() function. Create a python file with the following script that will print the floor value after calculating the result of a mathematical expression.

#Import math module
import math as mt

#Initialize four variables
num1 = 75.34
num2 = 56.89
num3 = 10.87
num4 = 5.23

#Print the floor value of the mathematical expression
print ("The floor value of (", num1,"+",num2,"-",num3,"*", num4,") expression is:", mt.floor(num1+num2-num3*num4))

Output:
The following output will appear after executing the above script. ## Example-4: Difference between floor() and int() functions

Create a python file with the following script to know the difference between the floor(), and int() functions for the positive and negative decimal numbers.

#Import math module
import math as mt

#Checking the floor and integer values of the same positive number
print('The floor value of 45.65 is ', mt.floor(45.65))
print('The integer value of 45.65 is ', int(45.65))

#Checking the floor and integer values of the same negative number
print('The floor value of -65.75 is ', mt.floor(-65.75))
print('The integer value of -65.75 is ', int(-65.75))

Output:
The following output will appear after executing the above script. The output shows that the output of the floor() and int() function varies for negative decimal numbers. ## Example-5: Using floor() for a list of numbers

Create a python file with the following script that will print the floor value of each element of a list. A list of positive and negative decimal numbers has been used in the script.

#Import math module
import math as mt

#Declare a list data
listdata = [7.89, -2.45, -4.34, 8.23, 0.56]

print("The list values after using floor() function")
#Print the floor value of the list data
for value in listdata:
print(mt.floor(value), end=' ')

print()

Output:
The following output will appear after executing the above script. Use of ceil() function:
The syntax of this function is given below.

Syntax:
int floor(number)

The function takes a number and returns an integer that is greater than or equal to the number.

## Example-1: Using ceil() function for positive number

Create a python file with the following script to print the ceil values of two different positive decimal numbers.

#Import ceil function from math
from math import ceil

#Print the ceil values of different positive decimal numbers
print ("The ceil value of 56.51 is: ", ceil(56.51))
print ("The ceil value of 67.66 is: ", ceil(67.66))

Output:
The following output will appear after executing the above script. ## Example-2: Using ceil() function for negative number

Create a python file with the following script to print the ceil values of two different negative decimal numbers.

#Import ceil function from math
from math import ceil

#Print the ceil values of different negative decimal numbers
print ("The ceil value of -98.50 is: ", ceil(-98.50))
print ("The ceil value of -76.75 is: ", ceil(-76.75))

Output:
The following output will appear after executing the above script. ## Example-3: Using ceil() function for mathematical expression

Like the floor() function, any mathematical expression can also be used as the ceil() function’s argument. Create a python file with the following script that will print the ceiling value after calculating the result of two mathematical expressions.

#Import ceil function from math
from math import ceil

#Print the ceil values of the mathematical expressions
print("The ceil value of (6.98","+","5.31) is ", ceil(6.98+5.31))
print("The ceil value of (13","/","3) is ", ceil(13/3))

Output:
The following output will appear after executing the above script. ## Example-4: Using ceil() function for a tuple

Create a python file with the following script that will print the ceiling value of each element of a tuple. A tuple of positive and negative decimal numbers has been used in the script.

#Import ceil function from math
from math import ceil

#Declare a tuple
numbers = (-56.4, 23.762, 0, -45.23, 61.72)

print("The tuple values before using ceil() function")
#Print the values of the tuple data
for value in numbers:
print(value, end=' ')

print()

print("The tuple values after using ceil() function")
#Print the ceil values of the tuple data
for value in numbers:
print(ceil(value), end=' ')

print()

Output:
The following output will appear after executing the above script. ## Conclusion:

The floor() and ceil() functions are used in the script when any mathematical task is required. How the floor() and ceil() functions work for different types of positive and negative decimal numbers, have been shown in this tutorial by using multiple examples. 