In C++, std::floor(x) rounds a number down. It returns the largest whole number that is not more than x. floor(3.8) is 3, and floor(3.0) stays 3.
"Down" always means toward smaller numbers, so a negative number moves away from zero: floor(-3.2) is -4. This is different from just cutting off the decimals with (int), which gives -3. The result is a double.
std::floor() is declared in <cmath> and is part of the group Rounding. Rounding functions turn a fractional number into a whole number. They differ in which way they go for negative numbers and for ties such as 2.5.
Syntax
Include <cmath> and call std::floor. Start with these lines.
C++
double a = std::floor(3.8); // double version
float b = std::floor(3.8f); // float version
Call
What it does
std::floor(3.8)
Passes a double and returns a double. Here the result is 3.
std::floor(3.8f)
Passes a float, so the float overload runs and returns a float.
std::floor(3)
An integer is converted to double. Returns 3 as a double.
Write std::floor so the C++ overloads for float, double, and long double are used.
Library declarations
C++
float floor(float x);
double floor(double x);
long double floor(long double x);
double floor(Integer x); // for integer types
Parameters
Parameter
Description
x
Required. The number to round down. It can be float, double, long double, or an integer type (converted to double).
Return Value
Return Value
Description
Rounded-down value
The largest whole number that is not more than x, as a floating-point type.
-0.0
floor(-0.0) returns -0.0.
x
If x is already a whole number, inf, or NaN, it is returned as it is.
C++ Examples
Example 1 — Round Several Numbers Down
Round a few numbers down. A whole number stays the same, and a negative number moves to the lower whole number.
C++
#include <cmath>
#include <iostream>
int main() {
double values[] = {3.8, 3.0, -3.2, 0.5};
for (double v : values) {
std::cout << "floor(" << v << ") = " << std::floor(v) << '\n';
}
return 0;
}
For positive 2.7 the cast and floor() agree. Both give 2.
The cast cuts off the decimals, so -2.7 becomes -2. That is a move up toward zero, which is not a round down.
floor(-2.7) gives -3, the next whole number below. Choose the one that matches your meaning.
Example 3 — Find the Grid Cell of a Position
A map is split into cells that are 10 units wide. The cell number of a position is floor(x / size). This also works for negative positions.
C++
#include <cmath>
#include <iostream>
int main() {
double size = 10.0;
for (double x : {3.5, 27.9, 10.0, -4.2}) {
int cell = static_cast<int>(std::floor(x / size));
std::cout << "x = " << x << " -> cell " << cell << '\n';
}
return 0;
}
Output
x = 3.5 -> cell 0
x = 27.9 -> cell 2
x = 10 -> cell 1
x = -4.2 -> cell -1
How It Works
Dividing the position by the cell size gives a decimal such as 0.35. floor() keeps only the whole part, which is the cell number.
27.9 gives cell 2, and 10.0 sits exactly on the border and starts cell 1.
The position -4.2 gives -0.42, and floor() moves it down to cell -1. A plain cast would wrongly say 0 and merge two cells.
Frequently Asked Questions
std::floor(x) rounds x down to the largest whole number that is less than or equal to x. So floor(3.8) is 3 and floor(-3.2) is -4.
The rounded-down value as a floating-point number (not an int). floor(3.8) returns 3.0, and a number that is already whole is returned unchanged.
It returns -3. floor() always goes toward smaller numbers, so a negative number moves away from zero. ceil(-2.5) returns -2.
Only for positive numbers. For negative numbers a cast cuts off the decimals and moves toward zero, while floor() goes down. Also, floor() returns a double, and the cast gives an int.
Include <cmath> and write std::floor(). That always works and picks the C++ overloads. The global name ::floor often works too, but the standard does not promise it, and it may only take a double.