floor() and ceil() function Python (original) (raw)

Last Updated : 3 Jun, 2026

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

import math

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

`

**Explanation:

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:

Compare floor() and ceil() with Negative Numbers

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

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:

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))

`

**Explanation:

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: