In this C++ tutorial, you will learn how to round a given number to nearest long int value using lround() function of cmath, with syntax and examples.
C++ lround()
C++ lround() returns the long int value nearest to the given value.
Syntax
The syntax of C++ lround() is
lround(x)
where
Parameter | Description |
---|---|
x | A double, float, long double or integral type value. |
Returns
The function returns value of type long int.
The synopsis of lround() function is
long int lround(float x);
long int lround(double x);
long int lround(long double x);
lround() is a function of cmath library. Include cmath library at the start of program, if using lround() function.
Example
In this example, we read a double value from user into variable x, and find the nearest long integer to this value in x, using round().
C++ Program
#include <iostream>
#include<cmath>
using namespace std;
int main() {
double x;
cout << "Enter a number : ";
cin >> x;
double result = lround(x);
cout << "lround(" << x << ") : " << result << endl;
}
Output
Enter a number : 1.96233699998745455
lround(1.96234) : 2
Program ended with exit code: 0
Enter a number : -1.24210
lround(-1.2421) : -1
Program ended with exit code: 0
Conclusion
In this C++ Tutorial, we learned the syntax of C++ lround(), and how to use this function to round the given value to nearest long int, with the help of examples.