C++取整

#include <iostream>
#include <cstdio>
#include <cmath>
//ceil()、floor()、round()包含在cmath库中,int()为强制类型转换
using namespace std;
int main(){
    //ceil()上取整,朝正无穷方向取整
    cout<<ceil(-2.8)<<' '<<ceil(-2.2)<<' '<<ceil(2.2)<<' '<<ceil(2.8)<<endl;
    printf("%.0lf %.0lf %.0lf %.0lf\n",ceil(-2.8),ceil(-2.2),ceil(2.2),ceil(2.8)); //ceil返回double 
    //输出-2 -2 3 3
    //floor()下取整,朝负无穷方向取整
    cout<<floor(-2.8)<<' '<<floor(-2.2)<<' '<<floor(2.2)<<' '<<floor(2.8)<<endl;
    printf("%.0lf %.0lf %.0lf %.0lf\n",floor(-2.8),floor(-2.2),floor(2.2),floor(2.8)); 
    //输出-3 -3 2 2
    //int()向零取整
    cout<<int(-2.8)<<' '<<int(-2.2)<<' '<<int(2.2)<<' '<<

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值