C ++中的round()函数返回最接近参数的整数值,在中间的情况从零舍入。
double round(double x); float round(float x); long double round(long double x); double round(T x); // 为整型
round()函数采用单个参数,并返回double,float或long double类型的值。此函数在<cmath>头文件中定义。
round()函数采用单个参数值进行舍入。
round()函数返回最接近x的整数值,在中间情况下从零舍入。
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
double x = 11.16, result;
result = round(x);
cout << "round(" << x << ") = " << result << endl;
x = 13.87;
result = round(x);
cout << "round(" << x << ") = " << result << endl;
x = 50.5;
result = round(x);
cout << "round(" << x << ") = " << result << endl;
x = -11.16;
result = round(x);
cout << "round(" << x << ") = " << result << endl;
x = -13.87;
result = round(x);
cout << "round(" << x << ") = " << result << endl;
x = -50.5;
result = round(x);
cout << "round(" << x << ") = " << result << endl;
return 0;
}运行该程序时,输出为:
round(11.16) = 11 round(13.87) = 14 round(50.5) = 51 round(-11.16) = -11 round(-13.87) = -14 round(-50.5) = -51
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int x = 15;
double result;
result = round(x);
cout << "round(" << x << ") = " << result << endl;
return 0;
}运行该程序时,输出为:
round(15) = 15
对于整数值,应用round函数将返回与输入相同的值。所以它在实际中并不常用来表示整数值。