C/C++取整

C/C++取整

需要引入#include<cmath>头文件,下面各个函数的返回值均为double

1
2
3
4
5
6
7
8
9
10
11
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
double a=2.5;
cout<<ceil(a)<<endl; //向上取整
cout<<floor(a)<<endl; //向下取整
cout<<round(a)<<endl; //四舍五入
return 0;
}

输出结果:

1
2
3
3
2
3