C++ std::floor() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
<cmath>

Definition

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
CallWhat 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

ParameterDescription
xRequired. The number to round down. It can be float, double, long double, or an integer type (converted to double).

Return Value

Return ValueDescription
Rounded-down valueThe largest whole number that is not more than x, as a floating-point type.
-0.0floor(-0.0) returns -0.0.
xIf 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;
}

How It Works

  1. The loop passes each number in the array to std::floor().
  2. 3.8 goes down to 3, and 3.0 is already whole, so it stays 3. 0.5 goes down to 0.
  3. -3.2 goes down to -4, which is further from zero. This is the part that surprises beginners.

Example 2 — floor() vs a Cast to int

Compare what a cast and floor() do to a positive and a negative number.

C++
#include <cmath>
#include <iostream>

int main() {
    double a = 2.7;
    double b = -2.7;

    std::cout << "(int)(2.7)   = " << static_cast<int>(a) << '\n';
    std::cout << "floor(2.7)   = " << std::floor(a) << '\n';
    std::cout << "(int)(-2.7)  = " << static_cast<int>(b) << '\n';
    std::cout << "floor(-2.7)  = " << std::floor(b) << '\n';
    return 0;
}

How It Works

  1. For positive 2.7 the cast and floor() agree. Both give 2.
  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.
  3. 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;
}

How It Works

  1. 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.
  2. 27.9 gives cell 2, and 10.0 sits exactly on the border and starts cell 1.
  3. 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.

Next: fma()

Keep going through the <cmath> functions. Multiply and add in one step: x * y + z.

fma() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful