English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

C++ llround() function usage and example

C++ Library Function <cmath>

C ++The llround() function in rounds a floating-point value to the nearest integer. The returned value is of type long long int. It is similar tolround()function, but returns long long int, while lround returns long int.

llround() prototype [from C ++ 11standard [begin]

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 integer

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

llround() parameters

The llround() function rounds a single parameter value to an integer.

llround() return value

The llround() function returns the nearest integer value to x, rounding towards zero in the middle case. The returned value is of type long long int.

Example1:llround() in C ++How does it work?

#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 the program is run, the output is:

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

Example2:The llround() function of integer type

#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 the program is run, the output is:

llround(15) = 15

For integer values, the llround function returns the same value as the input. Therefore, it is not commonly used to represent integer values in practice.

C++ Library Function <cmath>