问题遇到的现象和发生背景 保留小数位数出错
问题相关代码,请勿粘贴截图 const double PI=3.14159;
double r,s,v;
cout<<"请输入半径:";
cin>>r;
s=4*PI*r*r;
v=4*PI*r*r*r/3;
cout<<"圆球面积为:"<<setprecision(2)<<s<<endl;
cout<<"圆球体积为:"<<setprecision(4)<<v<<endl;
system("pause");
return 0;
}
double r,s,v;
cout<<"请输入半径:";
cin>>r;
s=4*PI*r*r;
v=4*PI*r*r*r/3;
cout<<"圆球面积为:"<<setprecision(2)<<s<<endl;
cout<<"圆球体积为:"<<setprecision(4)<<v<<endl;
system("pause");
return 0;
}
cout << "圆球面积为:" << setprecision(2) << fixed << s << endl;
cout << "圆球体积为:" << setprecision(4) << v << endl;
控制小数点精度用fixed
请输入半径:4
圆球面积为:201.06
圆球体积为:268.0823