C++取近似值简单算法

#include<iostream>
using namespace std;

//输入5.5,输出6,如果小数点后数值大于等于5,向上取整;小于5,则向下取整。
int main(){
float f;
cin>>f;
if((f-int(f))>=0.5){
cout<<int(f)+1;
}else{
cout<<int(f);
}
//下面为另一种有趣的方式实现

/*

float a;

cin >> a;

cout << int(a + 0.5); 

*/

return 0;

}

原文地址:https://www.cnblogs.com/xufeng123/p/12628712.html