C++ std::round() Function

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

Definition

In C++, std::round(x) rounds a number to the nearest whole number. round(2.4) is 2 and round(2.6) is 3. It is the rounding you learned at school.

The only special case is a number exactly halfway between two whole numbers. round() sends it away from zero, so 2.5 becomes 3 and -2.5 becomes -3. The result is a double. It was added in C++11.

std::round() 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::round. Start with these lines.

C++
double a = std::round(2.5);    // double version
float  b = std::round(2.5f);   // float version
CallWhat it does
std::round(2.5)Passes a double and returns a double. Here the result is 3.
std::round(2.5f)Passes a float, so the float overload runs and returns a float.
std::round(3)An integer is converted to double. Returns 3 as a double.

Write std::round so the C++ overloads for float, double, and long double are used.

Library declarations
C++
float       round(float x);
double      round(double x);
long double round(long double x);
double      round(Integer x);   // for integer types

Parameters

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

Return Value

Return ValueDescription
Rounded valueThe nearest whole number, as a floating-point type. Halves go away from zero.
-0.0A small negative number such as -0.4 gives -0.0.
xIf x is already whole, inf, or NaN, it is returned as it is.

C++ Examples

Example 1 — Round Several Numbers

Round numbers below, at, and above one half, and a negative half.

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

int main() {
    double values[] = {2.4, 2.5, 2.6, -2.5};

    for (double v : values) {
        std::cout << "round(" << v << ") = " << std::round(v) << '\n';
    }
    return 0;
}

How It Works

  1. The loop gives each number in the array to std::round().
  2. 2.4 is closer to 2, so it rounds down. 2.6 is closer to 3, so it rounds up.
  3. 2.5 is exactly halfway and goes away from zero to 3. -2.5 also goes away from zero, to -3.

Example 2 — Round to Two Decimal Places

Multiply by 100, round to a whole number, and divide by 100 again.

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

int main() {
    double values[] = {3.14159, 2.71828, 1.005};

    for (double v : values) {
        double rounded = std::round(v * 100.0) / 100.0;
        std::cout << v << " -> " << rounded << '\n';
    }
    return 0;
}

How It Works

  1. Multiplying by 100 moves the first two decimals before the point: 3.14159 becomes 314.159.
  2. round() turns 314.159 into 314, and dividing by 100 gives 3.14. The same steps turn 2.71828 into 2.72.
  3. 1.005 is stored as 1.00499999999999989, so v * 100 is just below 100.5 and the result is 1. This is not a bug in round(), but a limit of decimals in binary.

Example 3 — round() vs rint() on Halves

Both functions round to the nearest whole number, but they treat a number exactly halfway differently.

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

int main() {
    for (double x : {0.5, 1.5, 2.5, 3.5}) {
        std::cout << x << ": round " << std::round(x)
                  << ", rint " << std::rint(x) << '\n';
    }
    return 0;
}

How It Works

  1. round() always sends a half away from zero, so the answers are 1, 2, 3, and 4.
  2. rint() uses the current rounding mode, which is normally "to the nearest even number". So 0.5 goes to 0 and 2.5 goes to 2.
  3. The two functions agree for 1.5 and 3.5, because the even neighbour is also the one above. Choose round() for school-style rounding.

Frequently Asked Questions

std::round(x) rounds x to the nearest whole number. A number exactly halfway, such as 2.5, goes away from zero, so round(2.5) is 3 and round(-2.5) is -3.
The nearest whole number as a floating-point value. Halves are rounded away from zero, no matter what the current rounding mode is.
It rounds half away from zero. round(0.5) is 1, round(1.5) is 2, round(2.5) is 3, and round(-2.5) is -3.
Use std::round(x * 100) / 100. To only print two decimals without changing the value, use std::fixed and std::setprecision(2) from <iomanip>.
Include <cmath> and write std::round(). That always works and picks the C++ overloads. The global name ::round often works too, but the standard does not promise it, and it may only take a double.

Next: scalbln()

Keep going through the <cmath> functions. Like scalbn(), but the power is a long.

scalbln() 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.

12 people found this page helpful