C++ std::lround() Function

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

Definition

In C++, std::lround(x) rounds a decimal to the nearest whole number and gives it to you as a long. It is std::round() with an integer result, so no cast is needed.

A number exactly halfway goes away from zero: lround(2.5) is 3 and lround(-2.5) is -3. The current rounding mode is ignored. If the number is too large for a long, the result is not defined.

std::lround() 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::lround. Start with these lines.

C++
long a = std::lround(2.5);    // 3
long b = std::lround(-2.5);   // -3
long c = std::lround(2.4);    // 2
CallWhat it does
std::lround(2.5)Exactly halfway, so it goes away from zero. Returns 3.
std::lround(-2.5)Away from zero for a negative number means -3.
std::lround(2.4f)The float overload also returns a long. The result is 2.

The return type is always long. Use llround() for long long.

Library declarations
C++
long lround(float x);
long lround(double x);
long lround(long double x);
long lround(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
longThe nearest whole number. Halves are rounded away from zero.
UnspecifiedIf the result is out of range for a long, or x is NaN or infinity.

C++ Examples

Example 1 — Round Several Numbers to a long

The results are whole numbers of type long. Halves go away from zero.

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

int main() {
    double values[] = {2.5, -2.5, 2.4, 2.6, -0.4};

    for (double v : values) {
        long r = std::lround(v);
        std::cout << "lround(" << v << ") = " << r << '\n';
    }
    return 0;
}

How It Works

  1. The loop calls std::lround() and stores the answer in a long, so it prints as a plain whole number.
  2. 2.5 is a half and goes away from zero to 3. -2.5 goes away from zero too, to -3.
  3. 2.4 rounds down to 2, and 2.6 rounds up to 3. -0.4 rounds to 0, printed without a minus sign because it is an integer.

Example 2 — Average Rating to Whole Stars

A site shows whole stars. Round each average rating to the nearest star, with a half going up.

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

int main() {
    double ratings[] = {4.5, 3.49, 3.5, 1.2};

    for (double r : ratings) {
        long stars = std::lround(r);
        std::cout << r << " -> " << stars << " star(s)\n";
    }
    return 0;
}

How It Works

  1. Each average rating goes into lround(), and the whole number it returns is the star count.
  2. 4.5 is a half and goes up to 5. 3.5 also goes up to 4, but 3.49 stays at 3.
  3. 1.2 is far from a half and rounds down to 1. With lrint() the value 4.5 would give 4 instead.

Example 3 — Average Score to a Whole Grade

Add up the scores, divide, and round the average to a whole grade.

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

int main() {
    int first[] = {85, 90, 78, 92};
    int second[] = {70, 71};

    double avg1 = (85 + 90 + 78 + 92) / 4.0;
    double avg2 = (70 + 71) / 2.0;

    std::cout << "Average " << avg1 << " -> grade " << std::lround(avg1) << '\n';
    std::cout << "Average " << avg2 << " -> grade " << std::lround(avg2) << '\n';
    return 0;
}

How It Works

  1. The divisions use 4.0 and 2.0, so the averages keep their decimals: 86.25 and 70.5.
  2. 86.25 is closer to 86, so lround() returns 86.
  3. 70.5 is exactly halfway and goes away from zero, up to 71. A student with a half point average gets the higher grade.

Frequently Asked Questions

std::lround(x) rounds x to the nearest whole number and returns it as a long. A half goes away from zero, so lround(2.5) is 3 and lround(-2.5) is -3.
The nearest whole number as a long, with halves rounded away from zero. If the result does not fit in a long, the value is unspecified.
They round the same way, halves away from zero. round() returns a double like 3.0, and lround() returns a long like 3, so you do not need a cast.
The result is unspecified and an invalid exception is raised. Check the range first, or use llround() for bigger numbers.
Include <cmath> and write std::lround(). That always works and picks the C++ overloads. The global name ::lround often works too, but the standard does not promise it, and it may only take a double.

Next: modf()

Keep going through the <cmath> functions. Split a number into whole and fractional parts.

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

11 people found this page helpful