Skip to content
Related Articles

Related Articles

Improve Article

nearbyint() function in C++

  • Difficulty Level : Basic
  • Last Updated : 28 Sep, 2018

The nearbyint() function is defined in cmath header file. This function round off the given value to a nearby integral value by using the current rounding method. The current rounding method is described by fegetround.

Syntax:

float nearbyint(float x);

or,
double nearbyint(double x);

or,
long double nearbyint(long double x);

Parameters: This function accepts single parameter x which contains floating point value.

Return Value: It returns the rounded value of x to a nearby integral value by using the rounding method.

Below programs illustrate the nearbyint() function in C++:



Program 1:




// C++ program to demonstrate
// example of nearbyint() function.
  
#include <bits/stdc++.h>
using namespace std;
int main()
{
    float x = 2.7;
      
    cout << "Value of x = " << x << endl;
    cout << "Round off value of x = " << nearbyint(x);
      
    return 0;
}
Output:
Value of x = 2.7
Round off value of x = 3

Program 2:




// C++ program to demonstrate
// example of nearbyint() function.
  
#include <bits/stdc++.h>
using namespace std;
int main()
{
    double x = 3.2;
  
    cout << "Value of x = " << x << endl;
    cout << "Round off value of x = " << nearbyint(x);
  
    return 0;
}
Output:
Value of x = 3.2
Round off value of x = 3
Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for the language and STL. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.



My Personal Notes arrow_drop_up