In C++, std::llrint(x) rounds a decimal and returns a long long, which is at least 64 bits. It follows the current rounding mode, normally "to the nearest, with halves going to the even number". So llrint(2.5) is 2 and llrint(3.5) is 4.
It is the big brother of lrint(). On some systems a long only has 32 bits, so llrint() is the safe choice for large numbers such as byte counts, timestamps, or money in cents.
std::llrint() 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::llrint. Start with these lines.
C++
long long a = std::llrint(2.5); // 2
long long b = std::llrint(3000000000.4); // 3000000000
long long c = std::llrint(-7.5); // -8
Call
What it does
std::llrint(2.5)
Exactly halfway. The even neighbour 2 is chosen.
std::llrint(3000000000.4)
A value bigger than a 32-bit int. Returns 3000000000.
std::llrint(-7.5f)
The float overload also returns a long long. The result is -8.
The return type is always long long. For smaller numbers, lrint() returns long.
Library declarations
C++
long long llrint(float x);
long long llrint(double x);
long long llrint(long double x);
long long llrint(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
long long
The nearest whole number according to the current rounding mode.
Unspecified
If the result is out of range for a long long, or x is NaN or infinity.
C++ Examples
Example 1 — Round Several Numbers to a long long
Include one number that is too big for a 32-bit integer. Print the inputs with one decimal so you can see the fractions.
C++
#include <cmath>
#include <iomanip>
#include <iostream>
int main() {
double values[] = {2.5, 3.5, 3000000000.4, -7.5};
std::cout << std::fixed << std::setprecision(1);
for (double v : values) {
long long r = std::llrint(v);
std::cout << "llrint(" << v << ") = " << r << '\n';
}
return 0;
}
19.99 is stored as 19.989999999999998..., so 19.99 * 100 is 1998.9999999999998, not exactly 1999.
The cast cuts off the decimals and gives 1998, which is a whole cent too low. 4.35 and 0.57 have the same problem.
llrint() rounds to the nearest cent and gives 1999, 435, and 57. This is the safe way to turn dollars into cents.
Frequently Asked Questions
std::llrint(x) rounds x to a whole number using the current rounding mode and returns it as a long long. By default halves go to the even number.
The rounded value as a long long. If the result does not fit, or x is NaN or infinity, the value is unspecified and an invalid exception is raised.
Use llrint() when the number can be bigger than about two billion, because a long is only 32 bits on some systems, such as Windows. A long long is at least 64 bits.
Use std::llrint(price * 100). A plain cast cuts decimals like 1998.9999999999998 down to 1998, but llrint() rounds to the nearest cent.
Include <cmath> and write std::llrint(). That always works and picks the C++ overloads. The global name ::llrint often works too, but the standard does not promise it, and it may only take a double.