C++ cmath lrint() Function
Example
Round numbers to a long integer:
cout << lrint(6.2) << "\n";
cout << lrint(6.5) << "\n";
cout << lrint(7.5);
Definition and Usage
The lrint() function rounds a number to an integer, and returns it as a long.
It uses the current rounding mode (a setting that decides how numbers are rounded). By default, numbers are rounded to the nearest integer, and halfway cases are rounded to the nearest even integer. So 6.5 becomes 6, and 7.5 becomes 8.
The lrint function is defined in the <cmath> header file.
Tip: Use lround() to round halfway cases away from zero, like 6.5 to 7.
Syntax
lrint(double x)
The function also works with float and long double values.
Parameter Values
| Parameter | Description |
|---|---|
| x | Required. The number to round. |
Technical Details
| Returns: | A long value with the rounded number. |
|---|
Related Pages
Read more about math in our Math Tutorial.