从来就没有救世主  也不靠神仙皇帝  要创造人类的幸福  全靠我们自己  

C++标准库使用(细节)

 

1. cout控制输出浮点数位数

  头文件 iomanip

  默认6位有效数字;setprecision为设置有效位数,加上fixed就是设置小数位数,并且设置之后对后面的默认输出有效

double x = 0.03456789;
cout<<x<<endl;   //6位有效数字  0.0345679
cout<<setprecision(4)<<x<<endl; //0.03457
cout<<setprecision(8)<<x<<endl; //0.03456789  不够8位,不管
cout<<fixed<<setprecision(4)<<x<<endl; //0.0346 4位小数
cout<<x<<endl;  //4位小数 0.0346
cout.unsetf(ios::fxed); //去掉fixed设置
cout<<x<<endl;   //4位有效数字 0.03457

 

  设置必须输出小数点:

cout<<setiosflags(ios::fixed|ios::showpoint)<<setprecision(1)<<x;

 

posted @ 2020-04-05 19:14  T,X  阅读(285)  评论(0编辑  收藏  举报