floor() and ceil() function Python

Last Updated : 3 Jun, 2026

Python provides the floor() and ceil() functions in the math module to round numbers.

  • floor() rounds a number down to the nearest integer.
  • ceil() rounds a number up to the nearest integer.
Python
import math

x = 3.7
print(math.floor(x))  
print(math.ceil(x))

Output
3
4

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 return an integer.

Round a List of Floats

In this example, floor() and ceil() are applied to each element of a list using the map() function.

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, 2, 3, 4, 5]
Ceil : [2, 3, 4, 4, 6]

Explanation:

  • map(math.floor, a) applies floor() to each element of the list.
  • map(math.ceil, a) applies ceil() to each element of the list.
  • floor() rounds values down, while ceil() rounds values up to the nearest integer.

Compare floor() and ceil() with Negative Numbers

For negative numbers, floor() and ceil() behave differently compared to positive numbers.

  • floor() moves to the smaller integer.
  • ceil() moves to the larger integer.
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
ceil(-2.3) : -2
floor(-5.9): -6
ceil(-5.9) : -5

Explanation:

  • floor() always rounds towards negative infinity.
  • ceil() always rounds towards positive infinity.

Round User Input to Nearest Integer

floor() and ceil() functions can be used to round a decimal number down or up to the nearest integer. In this example, floor() rounds the number downward, while ceil() rounds it upward.

Python
import math

a = 7.3
print(math.floor(a))
print(math.ceil(a))

Output
7
8

Explanation:

  • math.floor(7.3) returns 7 because it rounds the number down.
  • math.ceil(7.3) returns 8 because it rounds the number up.

Computing Floor and Ceil Without Importing math

We can also find the floor and ceil values of a number without using the math module. This can be done using integer division (//) and simple conditions.

Note: Python’s // operator already works like floor() for both positive and negative numbers.

Floor and Ceil using Integer Division

Python
x = 4.5
y = -4.5

# Floor values
fx = x // 1
fy = y // 1

# Ceil values
cx = x // 1 if x == x // 1 else x // 1 + 1
cy = y // 1 if y == y // 1 else y // 1 + 1

print("Floor of 4.5:", fx)
print("Ceil of 4.5 :", cx)

print("Floor of -4.5:", fy)
print("Ceil of -4.5 :", cy)

Output
Floor of 4.5: 4.0
Ceil of 4.5 : 5.0
Floor of -4.5: -5.0
Ceil of -4.5 : -4.0

Explanation:

  • x // 1 gives the floor value of the number.
  • If the number is not already an integer, adding 1 gives the ceil value.
  • For negative numbers, Python automatically handles floor division correctly.
Comment