计算2个小数相加值,并且做四舍五入输出。请编写代码
四舍五入保留几位小数啊?
如果保留1位小数,结果=a+b+0.05
如果保留2位小数,结果=a+b+0.005
如果保留3位小数,结果=a+b+0.0005
参考代码如下:
#include <stdio.h>
int main()
{
float a,b,c;
scanf("%f %f",&a,&b);
//如果保留2位小数,就把结果+0.005,这样就能四舍五入,如果保留3位小数,结果+0.0005
c = a + b + 0.005;
printf("%f+%f=%.2f\n",a,b,c);
return 0;
}
懒得编译了,直接问答框里面敲代码了
#include<bits/stdc++.h>
using namespace std;
int main()
{
double a,b;
cin>>a>>b;
cout<<fixed<<setprecision(0)<<a+b;
return 0;
}