C++ llround()

The llround() function in C++ rounds the integer value that is nearest to the argument, with halfway cases rounded away from zero.

The llround() function in C++ rounds the integer value that is nearest to the argument, with halfway cases rounded away from zero. The value returned is of type long long int. It is similar to the lround() function, but returns a long long int whereas lround returns long int.


llround() prototype [As of C++ 11 standard]

long long int llround(double x);
long long int llround(float x);
long long int llround(long double x);
long long int llround(T x); // For integral type

The llround() function takes a single argument and returns a value of type long long int. This function is defined in <cmath> header file.


llround() Parameters

The llround() function takes a single argument value to round.


llround() Return value

The llround() function returns the integral value that is nearest to x, with halfway cases rounded away from zero. The value returned is of type long long int.


Example 1: How llround() works in C++?

#include <iostream>
#include <cmath>

using namespace std;

int main()
{   
    long long int result;
    double x = 11.16;
    result = llround(x);
    cout << "llround(" << x << ") = " << result << endl;

    x = 13.87;
    result = llround(x);
    cout << "llround(" << x << ") = " << result << endl;
    
    x = 50.5;
    result = llround(x);
    cout << "llround(" << x << ") = " << result << endl;
    
    x = -11.16;
    result = llround(x);
    cout << "llround(" << x << ") = " << result << endl;

    x = -13.87;
    result = llround(x);
    cout << "llround(" << x << ") = " << result << endl;
    
    x = -50.5;
    result = llround(x);
    cout << "llround(" << x << ") = " << result << endl;
    
    return 0;
}

When you run the program, the output will be:

llround(11.16) = 11
llround(13.87) = 14
llround(50.5) = 51
llround(-11.16) = -11
llround(-13.87) = -14
llround(-50.5) = -51

Example 2: llround() function for integral types

#include <iostream>
#include <cmath>

using namespace std;

int main()
{
    int x = 15;
    long long int result;
    result = llround(x);
    cout << "llround(" << x << ") = " << result << endl;

    return 0;
}

When you run the program, the output will be:

llround(15) = 15

For integral values, applying the llround function returns the same value as the input. So it is not commonly used for integral values in practice.