C/C++取整

C/C++取整

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

#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;
}

输出结果:

3
2
3

猜你喜欢

转载自blog.csdn.net/weixin_44338712/article/details/108139004