c++ 取整:四舍五入 向上取整 向下取整

对于数据的取整是经常需要考虑的 现在总结如下

#include<iostream>
#include<cmath>
using namespace std;
int main()
{
    double a=1.5;
    cout<<ceil(a)<<endl;   //向上取整
    cout<<floor(a)<<endl;   //向下取整
    cout<<round(a)<<endl;   //四舍五入
    //不使用函数   
    cout<<(int)a<<endl;  //向下取整
    cout<<(a>(int)a?(int)a+1:(int)a)<<endl;   //向上取整
    cout<<(int)(a+0.5)<<endl;//四舍五入
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/dyhaohaoxuexi/p/10928566.html