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