What is a ceiling in Python?
Ceil is a function in Python’s math module that takes a number as an argument and returns the ceiling as an integer that is greater than or equal to the number given as an argument.
It’s mathematically equivalent to the least integer.
What is ceiling and floor function in Python?
In Python, the math.floor() function rounds a number to the nearest and smallest integer and greater integer that returns the floor result.
It’s one of the mathematical functions in the math library.
Similarly, Python’s math.ceil() method returns the input value’s ceiling value.
Python ceil() Function
The ceiling() method rounds a number UP to the next integer, if needed, and returns the result.
Syntax:
import math math.ceil(x)
Parameter:
x:This is a numeric expression.
Returns:
Smallest integer not less than x.
Here is how the ceil() method works in Python:
For example:
# Python program to demonstrate the use of ceil() method
# This will import math module
import math
# prints the ceil using ceil() method
print "math.ceil(-23.11) : ", math.ceil(-23.11)
print "math.ceil(300.16) : ", math.ceil(300.16)
print "math.ceil(300.72) : ", math.ceil(300.72)
Output:
math.ceil(-23.11) : -23.0
math.ceil(300.16) : 301.0
math.ceil(300.72) : 301.0
Python floor() function
The floor() method rounds a number down to the closest integer, if necessary, and returns the result.
Syntax:
import math
math.floor(x)
Parameter:
x-numeric expression.
Returns:
largest integer not greater than x.
Here is how the floor() method works in Python:
For example:
# Python program to demonstrate the use of floor() method
# This will import math module
import math
# prints the ceil using floor() method
print "math.floor(-23.11) : ", math.floor(-23.11)
print "math.floor(300.16) : ", math.floor(300.16)
print "math.floor(300.72) : ", math.floor(300.72)
Output:
math.floor(-23.11) : -24.0
math.floor(300.16) : 300.0
math.floor(300.72) : 300.0
Python floor division
Floor division is an operation in Python that returns and divides two numbers and rounds the result down to the next integer.
The double backslash ( // ) operator does the floor division.
For example:
# app.py
print(5 // 3)
Output:
1
Divide and rounding to the nearest integer is known as floor division.
The nearest integer is used in the example above, which rounds down to 1. The result is 1 as a result.
Python floor to 2 decimal places
In Python, use the math.floor() function to round down to two decimal places.
Divide the output of the math.floor() method by 100.0 to get the floor value to two decimal places.
For example:
# app.py
import math
data = 19.2110
print(math.floor(data * 100) / 100.0)
Output:
19.21
Python floor without math
In Python, use the double-backslash ( // ) operator to compute the floor without using math. The double-slash operator is used for “floor” division, which rounds down to the next whole number.
For example:
# app.py
print(3 // 2)
Output:
1
Summary
This article discusses the Python ceiling. It also tackles the floor method, ceiling () function, floor() function, floor division, floor with 2 decimal places, and floor without math.
I hope this lesson has helped you learn a lot. Apart from this topic, you may also check: Python replace() string: Explanation with Example Program