C++ std::llrint() Function

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

Definition

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

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 according to the current rounding mode.
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

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;
}

How It Works

  1. std::fixed with setprecision(1) shows one decimal on the inputs, so a large number does not turn into 3e+09.
  2. 2.5 goes to the even 2, 3.5 goes to 4, and -7.5 goes to the even -8.
  3. 3000000000.4 rounds to 3000000000. That is more than a 32-bit int can hold, but a long long has no problem.

Example 2 — Gigabytes to Bytes

One gigabyte is 1024 * 1024 * 1024 bytes. Multiply a decimal size and round to a whole number of bytes.

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

int main() {
    const double bytesPerGB = 1024.0 * 1024.0 * 1024.0;

    for (double gb : {0.25, 1.5, 8.0}) {
        long long bytes = std::llrint(gb * bytesPerGB);
        std::cout << gb << " GB = " << bytes << " bytes\n";
    }
    return 0;
}

How It Works

  1. bytesPerGB is 1073741824. Multiplying by a decimal such as 1.5 gives a decimal result.
  2. llrint() turns 1610612736.0 into the whole number 1610612736 in a long long.
  3. 8.0 GB is 8589934592 bytes, which does not fit in a 32-bit integer. A long long has no such limit.

Example 3 — Money in Cents: Cast vs llrint()

Money is best stored as whole cents. A cast cuts 19.99 * 100 down to 1998, and llrint() fixes it.

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

int main() {
    double prices[] = {19.99, 4.35, 0.57};

    for (double p : prices) {
        long long cast = static_cast<long long>(p * 100);
        long long rounded = std::llrint(p * 100);
        std::cout << p << ": cast " << cast << ", llrint " << rounded << '\n';
    }
    return 0;
}

How It Works

  1. 19.99 is stored as 19.989999999999998..., so 19.99 * 100 is 1998.9999999999998, not exactly 1999.
  2. 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.
  3. 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.

Next: llround()

Keep going through the <cmath> functions. Round to the nearest long long, halves away from zero.

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

12 people found this page helpful