Ad space (banner)
🐍Python Lessons
Lesson 46 / 69

Rounding Numbers (floor, ceil, round)

This lesson covers rounding numbers using the math module and round(), so you can choose the right approach for the situation. It's written for anyone searching "Python floor ceiling round".

math.floor() rounds down, math.ceil() rounds up, and the built-in round() rounds to the nearest value (more precisely, "round half to even"). Think "shave it down," "round it up," and "round to the closer side." All three convert a decimal number to an integer, but it's important to correctly understand that they produce different results.

The sample code shows 3.7 becoming 3 with math.floor(), 3.2 becoming 4 with math.ceil(), and 3.5 becoming 4 with round(). Try different input values yourself to see which way each boundary case rounds.

A common beginner mistake is being surprised by Python's rounding quirk in round(). round(0.5) is 0, not 1 — a "round half to even" behavior different from the "always round .5 up" convention many people expect. Where accuracy matters, like amount calculations, it's worth double-checking this behavior.

Which rounding method you choose changes the result, so picking the right one for the purpose matters. Accidentally rounding up in an amount calculation can create a mismatch between the actual charge and the displayed amount — a real, practical concern that calls for care in choosing how to round.

Python
OUTPUT

💡 The Python engine may take a few seconds to load the first time you run code.

Ad space (banner)
Ad space (in-article)