C++——向上向下取整

本次博客,我将记录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


本文完

posted @ 2022-07-28 11:02  Sky6634  阅读(2289)  评论(0)    收藏  举报