AppDividend
Latest Code Tutorials

Python floor() and Python ceil() Function Example

0

Python floor() is an inbuilt function that returns the floor of x, the largest integer not greater than x. Python ceil() is an inbuilt method that returns the ceiling value of x, the smallest integer not less than x. The floor() method in Python returns the floor of the provided value. The ceil() method in Python returns the floor of the provided value. 

Python Floor and Ceil Function Example

Python math.floor() method is one of the Mathematical functions available in the Python math library.

The math.floor() function is used to return the nearest integer value which is less than or equal to a specified expression or value.

See the following syntax of the Python floor() method.

import math

math.floor(x)

It returns the smallest integer not less than x.

Let’s see the following code that demonstrates the Python floor method.

# app.py

import math

caltech = math.floor(300.72)
print(caltech)

First, We imported the math library using the import statement. It will allow us to use mathematical functions like the floor.

In the next line, we applied math floor function and passed the argument 300.72, and it will give us the output, and we have stored that output inside the Caltech variable and then display in the Python console.

See the output.

Python Floor and Ceil Function Tutorial

See the other following examples.

# app.py

import math

print("math.floor(-19.21): ", math.floor(-19.21))
print("math.floor(19.46): ", math.floor(19.46))
print("math.floor(46.19): ", math.floor(46.19))

An output of the above code is below.

Floor and Ceil Function

Python math.ceil() Function

Python ceil() method in Python returns ceiling value of x, the smallest integer not less than x.

See the following syntax.

import math

math.ceil(x)

It returns the smallest integer not less than x.

See the following coding example.

# app.py

import math

caltech = math.ceil(300.72)
print(caltech)

See the output.

Python math.ceil() Function

See the other following examples.

# app.py

import math

print("math.ceil(-19.63): ", math.ceil(-19.63))
print("math.ceil(19.63): ", math.ceil(19.63))
print("math.ceil(46.63): ", math.ceil(46.63))

The output of the above code is following.

Ceil Function Tutorial With Example

Finally, Python Floor and Ceil Function Tutorial With Example is over.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.