Python Rounding Demystified: The Difference Between int(), floor(), ceil(), and round()

Python Number Truncation & Rounding

Understanding the differences between int(), floor(), ceil(), and round() with floating-point numbers.

int()

Truncate

Simply drops the decimal part and moves toward zero. It is a built-in function.

floor()

Round Down

Rounds down to the nearest integer less than or equal to the number. Requires `import math`.

ceil()

Round Up

Rounds up to the nearest integer greater than or equal to the number. Requires `import math`.

round()

Standard Round

Rounds to the nearest even number if exactly halfway ("Round half to even"). It is a built-in function.

Input (Float) int(x) math.floor(x) math.ceil(x) round(x)
3.1 3 3 4 3
3.7 3 3 4 4
3.5 (Halfway) 3 3 4 4 (Nearest Even)

Comments