C++ std::llround() Function

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

Definition

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
CallWhat 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

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

Return Value

Return ValueDescription
long longThe nearest whole number. Halves are rounded away from zero.
UnspecifiedIf 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;
}

How It Works

  1. The inputs are printed with one decimal, so the big number shows as 3000000000.4 and not as 3e+09.
  2. 2.5 goes away from zero to 3, and -7.5 goes away from zero to -8. 0.4 goes to 0.
  3. 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;
}

How It Works

  1. Multiplying seconds by 1000 gives milliseconds with a decimal part, such as 1234.6.
  2. llround() turns 1234.6 into 1235. A tiny value like 0.0004 s is 0.4 ms, which rounds to 0.
  3. 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;
}

How It Works

  1. The timestamp is about 1.7 billion, more than a 32-bit int allows, so the result must be a long long.
  2. llround() rounds .6 up and gives 1700000001, the nearest whole second.
  3. 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.

Next: log()

Keep going through the <cmath> functions. Natural logarithm (base e) of a number.

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

13 people found this page helpful