int main()
{
float a = float (4) / 8;
float b = float(12 / 8);
float c = 545451.01;
cout << "a = "<< a <<endl;
cout << "b = "<< b << endl;
cout << "c = " << c << endl;
cout<<setprecision(3)<<12345.0<<endl;
cout << setiosflags(ios::fixed) << setprecision(1) << "c = " << c << endl;
cout << resetiosflags(ios::fixed);//取消固定格式输出
cout <<setprecision(6);
cout << "b = "<< b << endl;
cout << "c = " << c << endl;
cout << setprecision(1) << "c = " << c << endl;
cout << setprecision(1) << "b = " << b << endl;
system("pause");
return 0;
}
结果为:
这里面需要注意的setiosflags(ios::fixed)这句话,不错
如果和setprecision(n)一起用的话就是控制小数点后的位数了。
如果单独用setprecision(n),实际上是控制浮点数的有效数字个数
另外如果设置了setiosflags(ios::fixed)的话,需要人为的取消,要不然的话这个设置一直有效
取消就用resetiosflags(ios::fixed)