GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Python Tutorial
  • Data Types
  • Interview Questions
  • Examples
  • Quizzes
  • DSA Python
  • Data Science
  • NumPy
  • Pandas
  • Practice
  • Django
  • Flask
  • Math Module
  • Introduction1 min read

This view

  • Original page
  • New address

GeezerVison is showing www.geeksforgeeks.org — floor() and ceil() function Python - GeeksforGeeks

geeksforgeeks
search icon
  • Courses
  • Tutorials
  • Interview Prep
  • Python Tutorial
  • Data Types
  • Interview Questions
  • Examples
  • Quizzes
  • DSA Python
  • Data Science
  • NumPy
  • Pandas
  • Practice
  • Django
  • Flask

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.

Related Articles:

  • Python Tutorial
  • Math Module
Comment

Explore

    Python Fundamentals
    • Introduction1 min read
    • Input & Output2 min read
    • Variables4 min read
    • Operators4 min read
    • Keywords2 min read
    • Data Types4 min read
    • Conditional Statements3 min read
    • Loops3 min read
    • Functions4 min read
    Python Data Structures
    • String4 min read
    • List3 min read
    • Tuples3 min read
    • Dictionary3 min read
    • Set2 min read
    • Arrays5 min read
    Advanced Python
    • OOP Concepts3 min read
    • Exception Handling5 min read
    • File Handling4 min read
    • Python Database2 min read
    • MongoDB3 min read
    • MySQL7 min read
    • Packages4 min read
    • Modules3 min read
    • DSA Libraries5 min read
    • Python GUI2 min read
    Data Science with Python
    • Numpy3 min read
    • Pandas4 min read
    • Matplotlib3 min read
    • Seaborn3 min read
    • StatsModel2 min read
    • Model Building6 min read
    • TensorFlow2 min read
    • PyTorch5 min read
    Web Development with Python
    • Flask3 min read
    • Django5 min read
    • Django ORM4 min read
    • Jinja2 Templating5 min read
    • Django Templates5 min read
    • REST API3 min read
    • Build API with DRF4 min read
    Python Practice
    • Quiz1 min read
    • Practice Problems2 min read
    • Interview Q & A15+ min read
    Courses
    • Placement 360 Course2 min read
    • Data Science 360 Course2 min read
    • AI Engg Course2 min read
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.