In C++, std::llround(x) rounds a decimal to the nearest whole number and returns a long long, a type with at least 64 bits. It is std::round() with a big integer result.
A number exactly halfway goes away from zero: llround(2.5) is 3 and llround(-7.5) is -8. It ignores the current rounding mode. Use it for large values such as timestamps, milliseconds, and byte counts.
std::llround() 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::llround. Start with these lines.
C++
long long a = std::llround(2.5); // 3
long long b = std::llround(-7.5); // -8
long long c = std::llround(3000000000.4); // 3000000000
Call
What it does
std::llround(2.5)
Exactly halfway, so it goes away from zero. Returns 3.
std::llround(-7.5)
Away from zero for a negative number means -8.
std::llround(3000000000.4f)
The float overload also returns a long long, but a float only holds about 7 digits, so use double for big numbers.
The return type is always long long. For smaller numbers, lround() returns long.
Library declarations
C++
long long llround(float x);
long long llround(double x);
long long llround(long double x);
long long llround(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. Halves are rounded away from zero.
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
Round small values, a negative half, and a value that is too big for a 32-bit integer.
C++
#include <cmath>
#include <iomanip>
#include <iostream>
int main() {
double values[] = {2.5, -7.5, 3000000000.4, 0.4};
std::cout << std::fixed << std::setprecision(1);
for (double v : values) {
long long r = std::llround(v);
std::cout << "llround(" << v << ") = " << r << '\n';
}
return 0;
}
The inputs are printed with one decimal, so the big number shows as 3000000000.4 and not as 3e+09.
2.5 goes away from zero to 3, and -7.5 goes away from zero to -8. 0.4 goes to 0.
3000000000.4 rounds to 3000000000, a value that needs a long long on systems where long has 32 bits.
Example 2 — Seconds to Whole Milliseconds
A timer gives seconds as a decimal. Multiply by 1000 and round to get whole milliseconds.
C++
#include <cmath>
#include <iostream>
int main() {
double seconds[] = {1.2346, 0.0004, 123456.789};
for (double s : seconds) {
long long ms = std::llround(s * 1000.0);
std::cout << s << " s = " << ms << " ms\n";
}
return 0;
}
Output
1.2346 s = 1235 ms
0.0004 s = 0 ms
123457 s = 123456789 ms
How It Works
Multiplying seconds by 1000 gives milliseconds with a decimal part, such as 1234.6.
llround() turns 1234.6 into 1235. A tiny value like 0.0004 s is 0.4 ms, which rounds to 0.
cout prints the last input as 123457, which is only six digits, but the rounding used the full value and gives 123456789 ms.
Example 3 — Round a Timestamp to a Whole Second
A clock value is 1700000000.6 seconds. Round it to the nearest whole second.
C++
#include <cmath>
#include <iomanip>
#include <iostream>
int main() {
double now = 1700000000.6;
long long rounded = std::llround(now);
long long cut = static_cast<long long>(now);
std::cout << std::fixed << std::setprecision(1);
std::cout << now << " -> rounded " << rounded << '\n';
std::cout << now << " -> cut " << cut << '\n';
return 0;
}
The timestamp is about 1.7 billion, more than a 32-bit int allows, so the result must be a long long.
llround() rounds .6 up and gives 1700000001, the nearest whole second.
The cast cuts off the decimals and gives 1700000000, which is always rounded down. Choose rounding when you want the nearest second.
Frequently Asked Questions
std::llround(x) rounds x to the nearest whole number and returns it as a long long. A half goes away from zero, so llround(2.5) is 3 and llround(-7.5) is -8.
The nearest whole number as a long long, with halves rounded away from zero. If the result does not fit, the value is unspecified.
Both return a long long. llround() always sends a half away from zero. llrint() follows the current rounding mode, and halves go to the even number by default.
Pass a double for large values. A float only keeps about 7 digits, so a big number loses its decimals before rounding.
Include <cmath> and write std::llround(). That always works and picks the C++ overloads. The global name ::llround often works too, but the standard does not promise it, and it may only take a double.