floor() and ceil() function Python
Last Updated :
08 Apr, 2025
Python provides a built-in math module that includes many useful mathematical functions and constants. It is commonly used for operations such as rounding numbers, working with trigonometric functions, performing logarithmic calculations, and more."
Among these are two commonly used functions:
- floor(): Rounds a number down to the nearest integer, for example, floor() of 3.3 will be 3.
- ceil(): Rounds a number up to the nearest integer, for example, ceil() of 3.3 will be 4.
These functions are part of the math module, so you need to import it before using them. Let's look at an example:
Python
import math
x = 3.7
print(math.floor(x))
print(math.ceil(x))
Explanation:
- floor(3.7) returns 3 because 3 is the greatest integer less than or equal to 3.7.
- ceil(3.7) returns 4 because 4 is the smallest integer greater than or equal to 3.7.
Syntax
math.floor(number)
math.ceil(number)
Parameters:
- number: A float or integer value.
Return Type: Both functions return an integer value.
Examples of floor() and ceil()
Example 1: Round a List of Floats Down and Up
Let’s take a list of floating-point numbers and apply both floor() and ceil() to each value.
Python
import math
a = [1.1, 2.5, 3.9, 4.0, 5.8]
fl = list(map(math.floor, a))
cl = list(map(math.ceil, a))
print("Floor:", fl)
print("Ceil :", cl)
Output('Floor:', [1.0, 2.0, 3.0, 4.0, 5.0])
('Ceil :', [2.0, 3.0, 4.0, 4.0, 6.0])
Explanation:
- math.floor(1.1) returns 1, and math.ceil(1.1) returns 2, and so on.
- map() function applies floor and ceil to each element of the list.
Example 2: Compare floor() and ceil() with Negative Numbers
Python
import math
a = -2.3
b = -5.9
print("floor(-2.3):", math.floor(a))
print("ceil(-2.3) :", math.ceil(a))
print("floor(-5.9):", math.floor(b))
print("ceil(-5.9) :", math.ceil(b))
Output('floor(-2.3):', -3.0)
('ceil(-2.3) :', -2.0)
('floor(-5.9):', -6.0)
('ceil(-5.9) :', -5.0)
Explanation:
- floor() always rounds towards negative infinity.
- ceil() always rounds towards positive infinity.
Example 3: Round User Input to Nearest Integer.
Python
import math
a = float(input("Enter a number: "))
print("Rounded down using floor():", math.floor(a))
print("Rounded up using ceil():", math.ceil(a))
Suppose the user inputs 7.3, then the output will be:
Rounded down using floor(): 7
Rounded up using ceil(): 8
Computing Floor and Ceil Without Importing math
Apart from using the math module, we can also compute the floor and ceil of a float using basic arithmetic operations like floor division (//) and addition.
Concept:
- x // 1 returns the largest integer less than or equal to x - similar to math.floor(x).
- To get the ceiling, just add 1 to the floor value (i.e., x // 1 + 1).
Note: This method works well for positive numbers. For negative numbers, it may not give accurate ceiling values.
Example: Floor and Ceil using Integer Division
Python
x = 4.5
f = x // 1
print(f)
c = x // 1 + 1
print(c)
Explanation:
- 4.5 // 1 gives 4.0, which is the floor value.
- 4.5 // 1 + 1 gives 5.0, which is the ceil value.
floor() and ceil() function Python
Similar Reads
Python | math.floor() function In Python, math module contains a number of mathematical operations, which can be performed with ease using the module. math.floor() function returns the largest integer not greater than x. If number is already integer, same number is returned. Syntax: math.floor(x) Parameter: x: This is a numeric e
1 min read
Python | math.ceil() function In Python, math module contains a number of mathematical operations, which can be performed with ease using the module. math.ceil() function returns the smallest integral value greater than the number. If number is already integer, same number is returned. Syntax: math.ceil(x) Parameter: x: This is
1 min read
cmp() function - Python cmp() method in Python 2.x compares two integers and returns -1, 0, 1 according to comparison. cmp() does not work in python 3.x. You might want to see list comparison in Python.ExamplePython# Example 1 print(cmp(3, 4)) # Example 2 print(cmp(5, 5)) # Example 3 print(cmp(7, 6)) Output-101ExplanationI
3 min read
PHP ceil( ) Function We have often used the ceiling function in mathematical problems to round up a decimal number to next greater integral value. PHP provides us with a built-in function ceil() to perform such operation. The ceil() function is a built-in function in PHP and is used to round a number to the nearest grea
1 min read
Python | Decimal logical_and() method Decimal#logical_and() : logical_and() is a Decimal class method which returns the digit-wise and of the two (logical) Decimal values. Syntax: Decimal.logical_and() Parameter: Decimal values Return: the digit-wise and of the two (logical) Decimal values. Code #1 : Example for logical_and() method Pyt
2 min read
Precision Handling in Python Python in its definition allows handling the precision of floating-point numbers in several ways using different functions. Most of them are defined under the "math" module. In this article, we will use high-precision calculations in Python with Decimal in Python.ExampleInput: x = 2.4Output: Integra
6 min read