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
Call
What 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
Parameter
Description
x
Required. The number to round. It can be float, double, long double, or an integer type (converted to double).
Return Value
Return Value
Description
Rounded value
A whole number as a floating-point type, chosen by the current rounding mode.
x
If x is already whole, inf, or NaN, it is returned as it is.
FE_INEXACT
May 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;
}
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.
With FE_UPWARD every value goes toward bigger numbers: 2.1 becomes 3, and -2.1 becomes -2.
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.