floor() and ceil() function Python


These two methods are part of python math module which helps in getting the nearest integer values of a fractional number.

floor()

It accepts a number with decimal as parameter and returns the integer which is smaller than the number itself.

Syntax

Syntax: floor(x)
Where x is a numeric value

Example of floor()

In the below example we take different types of numeric values like, integer, positive decimal and negative decimal and apply the floor function to them. We get the nearest integer which is less than the numeric value supplied.

import math
x,y,z = 21 , -23.6 , 14.2
print("The value of ",x, "on applying floor() function is:", math.floor(x))
print("The value of ",y, "on applying floor() function is:", math.floor(y))
print("The value of ",z, "on applying floor() function is:", math.floor(z))

Output

Running the above code gives us the following result −

The value of 21 on applying floor() function is: 21
The value of -23.6 on applying floor() function is: -24
The value of 14.2 on applying floor() function is: 14

ceil()

It accepts a number with decimal as parameter and returns the integer which is greater than the number itself.

Syntax

Syntax: veil(x)
Where x is a numeric value

Example of ceil()

In the below example we take different types of numeric values like, integer, positive decimal and negative decimal and apply the ceil function to them. We get the nearest integer which is greater than the numeric value supplied.

import math
x,y,z = 21 , -23.6 , 14.2
print("The value of ",x, "on applying ceil() function is:", math.ceil(x))
print("The value of ",y, "on applying ceil() function is:", math.ceil(y))
print("The value of ",z, "on applying ceil() function is:", math.ceil(z))

Output

Running the above code gives us the following result −

The value of 21 on applying ceil() function is: 21
The value of -23.6 on applying ceil() function is: -23
The value of 14.2 on applying ceil() function is: 15

Updated on: 08-Aug-2019

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements