作者:Sky6634
如果要使用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
本文完。