c++中有这三个函数用来处理小数向整数的转换:
2.1 2.6 -2.1 -2.6
floor : 不大于自变量的最大整数 2 2 -3 -3
ceil :不小于自变量的最大整数 3 3 -2 -2
round:四舍五入到最邻近的整数 2 3 -2 -3
原文链接,在VS2012中编译时出现:error C3861: “round”: 找不到标识符,即使加了头文件cmath也不行,在代码中加上round模板即可。
#include <iostream>
using namespace std;
template<class T>
T round(T r)
{
return (r > 0.0) ? floor(r + 0.5) : ceil(r - 0.5);
}
int main()
{
double a=-37.7744256;
a=round(a*1000)/1000;
cout<<a<<endl;
return 0;
}