1.floor函数
功能:把一个小数向下取整
即就是如果数是2.2 ,那向下取整的结果就为2.000000
原型:double floor(doube x);
参数解释:
x:是需要计算的数
返回值:
成功:返回一个double类型的数,此数默认有6位小数
无失败的返回值
#include<iostream>
using namespace std;
int main()
{
double i = floor(2.2);
double j = floor(-2.2);
cout << "The floor of 2.2 is " << i << endl;
cout << "The floor of -2.2 is " << j << endl;
system("pause");
return 0;
}
运行结果:
2.ceil函数
功能:把一个小数向上取整
即就是如果数是2.2 ,那向上取整的结果就为3.000000
原型:double ceil(doube x);
参数解释:
x:是需要计算的数
返回值:
成功:返回一个double类型的数,此数默认有6位小数
无失败的返回值
头文件:#include<math.h>
#include<iostream>
using namespace std;
int main()
{
double i = ceil(2.2);
double j = ceil(-2.2);
cout << "The ceil of 2.2 is " << i << endl;
cout << "The ceil of -2.2 is " << j << endl;
system("pause");
return 0;
}
运行结果:
3.round函数
功能:把一个小数四舍五入
即就是如果数是2.2 ,那四舍五入的结果就为2
如果数是2.5,那结果就是3
原型:double round(doube x);
参数解释:
x:是需要计算的数
头文件:#include<math.h>
#include<iostream>
using namespace std;
int main()
{
double i = round(2.2);
double x = round(2.7);
double j = round(-2.2);
double y = round(-2.7);
cout << "The round of 2.2 is " << i << endl;
cout << "The round of 2.7 is " << x << endl;
cout << "The round of -2.2 is " << j << endl;
cout << "The round of -2.7 is " << y << endl;
system("pause");
return 0;
}
运行结果:
示例代码:设置double保留几位小数,并进行四舍五入
double Round(double value, short precision)
{
double uint = std::pow(.1, precision);
return std::round(value / uint) * uint;
}
int main()
{
cout << Round(3.14215231,3) << endl;
return 0;
}