AppDividend
Latest Code Tutorials

Python Floor and Ceil Function Tutorial With Example

0

Python Floor and Ceil Function Tutorial With Example is today’s topic. We need first to import the math module in Python to use the Python floor() and Python ceil() function. The floor() method in Python returns floor of x, the largest integer not greater than x. The ceil() method in Python returns ceiling value of x, the smallest integer not less than x.

Python Floor and Ceil Function Tutorial

Python math.floor() method is one of the Mathematical Function available in 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 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 the mathematical functions like 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.

The output is following.

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.

The syntax is following.

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)

The output is following.

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.