Python floor and ceil: The Complete Guide
The math.floor() method in Python rounds a number down to the nearest integer, if necessary, and returns the output. It is one of the Mathematical functions available in the math library.
Likewise, the math.ceil() method in Python returns the ceiling value of the input value. Let’s see these functions in detail with their syntax, parameters, and example.
Python floor
Python floor() is a built-in math library function that returns the floor of numeric input values. The math.floor() function takes a numeric number as an argument and returns the largest integer not greater than the input value.
The math.floor() function returns the nearest integer value, which is less than or equal to a specified expression or value.
To work with the floor() method, you need to import the math module from Python.
Syntax
import math math.floor(x)
Arguments
The math.floor() method takes x as a numeric number, a required parameter.
Example
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 the math.floor() function and passed the argument 300.72, which will give us the output, and we have stored that output inside the Caltech variable and then displayed it in the Python console.
See the output.
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.
Python floor division
Floor division in Python is an operation that divides two numbers and rounds the result down to the nearest integer. The floor division occurs via the double-backslash (//) operator.
# app.py print(5 // 3)
Output
1
Floor division means dividing and rounding down to the nearest integer. In the above example, the rounding down to the nearest integer is 1 that’s why the output is 1.
Python floor to 2 decimal places
To round down 2 decimal places in Python, use the math.floor() function. Divide the math.floor() function’s output by 100.0 and you will get the floor value to 2 decimal places.
# app.py import math data = 19.2110 print(math.floor(data * 100) / 100.0)
Output
19.21
Python floor without math
To calculate floor without math in Python, use the double-backslash (//) operator. The double-slash operator is used for “floor” division which rounds down to the nearest whole number.
# app.py print(3 // 2)
Output
1
Python ceil
Python ceil() is a built-in method that returns the ceiling value of the input value, the smallest integer not less than the input value. The ceil() method takes only one input value, the numeric value.
Syntax
import math math.ceil(x)
Arguments
The math.ceil() function takes x as an input numeric value.
Example
To work with the ceil() method, you need to import the math module from Python.
See the following coding example.
# app.py import math caltech = math.ceil(300.72) print(caltech)
See the output.
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 the following.
That is it for this tutorial.