The floor() function returns the largest integer that is less than or equal to a floating-point value. It always rounds toward negative infinity—the practical “round down” for positive numbers.
01
Floor
Round down −.
02
math.h
Link with -lm.
03
Returns double
Not int.
04
Negatives
Toward −∞.
05
vs ceil
Opposite.
06
vs trunc
On negatives.
Fundamentals
Definition and Usage
Mathematically, floor(x) is the largest integer n such that n ≤ x. On the number line, you move left (toward −∞) until you hit an integer.
For positive fractions like 5.78, that feels like “round down” to 5. For negatives, remember: floor(-2.3) is −3, not −2, because −3 is less than −2.3.
💡
Beginner Tip
Need only complete batches of 10 from 47 items? floor(47.0 / 10.0) gives 4 full batches—use ceil instead if partial batches still count as a page.
On negatives, floor and trunc diverge. Choose the direction that matches your business rule.
Applications
🚀 Common Use Cases
Conservative billing — charge only for complete units.
Grid indexing — map pixel position to tile row/column.
Inventory — count how many full crates fit in stock.
Time buckets — complete hours/minutes elapsed.
Pair with ceil() — bracket a value: floor(x) ≤ x ≤ ceil(x).
🧠 How floor() Works
1
Receive double x
Any finite floating-point input.
Input
2
Find integer ≤ x
Move left on the number line to the previous integer.
Compute
3
Return as double
Integral value stored in floating-point format.
Output
=
🔢
Floor value
Cast to int when you need a whole number for indexing.
Important
📝 Notes
Returns double, not int—cast explicitly when needed.
Rounds toward −infinity, not always “toward zero.”
floor(-2.3) is −3; do not assume “drop decimals.”
Link with -lm when using GCC/Clang on Unix-like systems.
Compare with ceil(), round(), and trunc() for other rounding needs.
Performance
⚡ Optimization
floor() is implemented in the platform math library. For positive values only, some code uses (int)x when truncation toward zero equals floor—but that breaks for negatives. Prefer floor unless profiling shows a hot path that needs micro-optimization.
Wrap Up
Conclusion
floor() gives the largest integer not greater than x, rounding toward negative infinity. Use it for complete batches, conservative rounding, and grid math—but pick ceil when partial units still count.
Continue with fmod() for remainders, or explore ceil() for the opposite rounding direction.
For positive numbers they match. For negatives, floor(-2.3) is -3 (toward -infinity) while trunc(-2.3) is -2 (toward zero). Pick based on rounding direction.
No. floor() returns double. Cast to int only when the value fits: int n = (int)floor(x); check range before casting huge values.
Did you know?
For any real x, floor(x) and ceil(x) are consecutive integers that bracket x: floor(x) ≤ x ≤ ceil(x). Together they define the integer part above and below any decimal.