C++ cmath nearbyint() Function
Example
Round numbers to an integer:
cout << nearbyint(4.3) << "\n";
cout << nearbyint(4.5) << "\n";
cout << nearbyint(5.5);
Definition and Usage
The nearbyint() function rounds a number to an integer, and returns it as a double.
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 4.5 becomes 4, and 5.5 becomes 6.
The nearbyint function is defined in the <cmath> header file.
Tip: rint() gives the same result. Use round() to round halfway cases away from zero instead.
Syntax
nearbyint(double x)
The function also works with float and long double values. Then it returns a value of the same type.
Parameter Values
| Parameter | Description |
|---|---|
| x | Required. The number to round. |
Technical Details
| Returns: | A double value with the rounded number. |
|---|
Related Pages
Read more about math in our Math Tutorial.