📜  C++中的nearint()函数

📅  最后修改于: 2021-05-30 12:56:40             🧑  作者: Mango

cmath头文件中定义了nearint()函数。此函数使用当前的舍入方法将给定值四舍五入为附近的整数值。当前的舍入方法由fegetround描述。

句法:

float nearbyint(float x);

or,
double nearbyint(double x);

or,
long double nearbyint(long double x);

参数:此函数接受包含浮点值的单个参数x

返回值:使用舍入方法将x的舍入值返回到附近的整数值。

下面的程序说明了C++中的nearingint()函数:

程序1:

// C++ program to demonstrate
// example of nearbyint() function.
  
#include 
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;
}
输出:
Value of x = 2.7
Round off value of x = 3

程式2:

// C++ program to demonstrate
// example of nearbyint() function.
  
#include 
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;
}
输出:
Value of x = 3.2
Round off value of x = 3
想要从精选的最佳视频中学习和练习问题,请查看有关从基础到高级C++的C++基础课程以及有关语言和STL的C++ STL课程。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”