C++ Math nearbyint
The function will approximate the provided value to the nearest whole number based on the current rounding method in place, which is defined by fegetround.
Syntax
Suppose a number is 'x'. Syntax would be:
Example
return_type nearbyint(data_type x);
Note: The return_type can be float, double or long double.
Parameter
x : The value can be float or double.
Return value
It provides the nearest whole number value for x by employing a specific rounding technique.
Example 1
Let's see a simple example.
Example
#include <iostream>
#include<math.h>
#include <cfenv>
using namespace std;
int main()
{
float x=2.3;
std::cout << "Value of x is:" <<x<< std::endl;
cout<<"Rounding to nearby,value is :"<<nearbyint(x);
return 0;
}
Output:
Output
Value of x is:2.3
Rounding to nearby,value is :2