本次博客,我将记录c++中的向上和向下取整
如果要使用c++自带的取整和四舍五入函数,则须要包含头文件
#include<cmath>
下面贴出代码即可说明:
//向下取整
cout<<"(int)1.2="<<(int)1.2<<endl;
cout<<"(int)1.8="<<(int)1.8<<endl;
cout<<"floor(1.2)="<<floor(1.2)<<endl;
cout<<"floor(1.8)="<<floor(1.8)<<endl<<endl;;
//向上取整
cout<<"(int)1.2+1="<<(int)1.2+1<<endl;
cout<<"(int)1.8+1="<<(int)1.8+1<<endl;
cout<<"ceil(1.2)="<<ceil(1.2)<<endl;
cout<<"ceil(1.8)="<<ceil(1.8)<<endl<<endl;;
//四舍五入到最近的整数
cout<<"(int)(1.2+0.5)="<<(int)(1.2+0.5)<<endl;
cout<<"(int)(1.8+0.5)="<<(int)(1.8+0.5)<<endl;
cout<<"round(1.2)="<<round(1.2)<<endl;
cout<<"round(1.8)="<<round(1.8)<<endl<<endl;
输出结果:
(int)1.2=1
(int)1.8=1
floor(1.2)=1
floor(1.8)=1
(int)1.2+1=2
(int)1.8+1=2
ceil(1.2)=2
ceil(1.8)=2
(int)(1.2+0.5)=1
(int)(1.8+0.5)=2
round(1.2)=1
round(1.8)=2
本文完
原文链接: https://www.cnblogs.com/Sky6634/p/16527879.html
欢迎关注
微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍
原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/190580
非原创文章文中已经注明原地址,如有侵权,联系删除
关注公众号【高性能架构探索】,第一时间获取最新文章
转载文章受原作者版权保护。转载请注明原作者出处!