C++ std::rint() Function

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

Definition

In C++, std::rint(x) rounds a decimal to a whole number using the current rounding mode. In the default mode that means "round to the nearest, and send a half to the even number". So rint(2.5) is 2, rint(3.5) is 4, and rint(2.6) is 3.

The result is a double. If you want a long or long long, use lrint() or llrint(). The only difference from nearbyint() is that rint() may raise the "inexact" flag, which most programs never look at.

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

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

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

Library declarations
C++
float       rint(float x);
double      rint(double x);
long double rint(long double x);
double      rint(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 valueA whole number as a floating-point type, chosen by the current rounding mode.
xIf x is already whole, inf, or NaN, it is returned as it is.
FE_INEXACTMay be raised when the result is not equal to x. Most programs ignore it.

C++ Examples

Example 1 — Round Several Numbers

Round some numbers. A half goes to the even whole number.

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

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

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

How It Works

  1. The loop passes each value to std::rint(), which uses the default mode "to nearest".
  2. 2.5 goes to the even 2, 3.5 goes to the even 4, and 0.5 goes to 0.
  3. -1.5 goes to -2, the even one. 2.6 is not a half, so it simply rounds to 3.

Example 2 — Use the Result as a Whole Number

Since rint() returns a double, cast it to int before you use it as a count or an index.

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

int main() {
    double scores[] = {7.5, 8.5, 9.5, 6.4};

    for (double s : scores) {
        int stars = static_cast<int>(std::rint(s));
        std::cout << s << " -> " << stars << " stars\n";
    }
    return 0;
}

How It Works

  1. rint(7.5) returns 8.0, because 8 is the even neighbour. The cast turns it into the int 8.
  2. 8.5 goes down to 8 because 8 is even, and 9.5 goes up to 10. Both halves go to an even number.
  3. 6.4 is not a half, so it goes to 6. The cast is safe here, because the value is small.

Example 3 — rint() Follows the Rounding Mode

Change the rounding mode with std::fesetround() from <cfenv> and run rint() again on the same numbers.

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

int main() {
    volatile double a = 2.1;
    volatile double b = -2.1;

    std::fesetround(FE_TONEAREST);
    std::cout << "nearest:  " << std::rint(a) << ", " << std::rint(b) << '\n';
    std::fesetround(FE_UPWARD);
    std::cout << "upward:   " << std::rint(a) << ", " << std::rint(b) << '\n';
    std::fesetround(FE_DOWNWARD);
    std::cout << "downward: " << std::rint(a) << ", " << std::rint(b) << '\n';

    std::fesetround(FE_TONEAREST);
    return 0;
}

How It Works

  1. The volatile keyword makes the compiler read the values at run time. In the default mode 2.1 rounds to 2 and -2.1 rounds to -2.
  2. With FE_UPWARD every value goes toward bigger numbers: 2.1 becomes 3, and -2.1 becomes -2.
  3. With FE_DOWNWARD every value goes toward smaller numbers, giving 2 and -3. The last line sets the mode back to normal.

Frequently Asked Questions

std::rint(x) rounds x to a whole number using the current rounding mode. By default it rounds to the nearest, with halves going to the even number, so rint(2.5) is 2.
The rounded value as a floating-point number. The result depends on the rounding mode, and the inexact flag may be raised.
The default rounding mode sends a value exactly halfway to the even whole number, and 2 is even. This is called banker's rounding. Use std::round() if you want 3.
rint() uses the current rounding mode, and by default halves go to the even number. round() always sends halves away from zero, whatever the mode is.
Include <cmath> and write std::rint(). That always works and picks the C++ overloads. The global name ::rint often works too, but the standard does not promise it, and it may only take a double.

Next: round()

Keep going through the <cmath> functions. Round a number to the nearest whole number.

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

11 people found this page helpful