C++浮點數的部分輸出格式

/*關於浮點數的格式*/  
#include <iostream.h>   
void  main()   
{   
float  f=2.0/3.0,f1=0.000000001,f2=-9.9;   
cout<<f<< ' ' <<f1<< ' ' <<f2<<endl;  //正常輸出   
cout.setf(ios::showpos);  //強制在正數前加+號   
cout<<f<< ' ' <<f1<< ' ' <<f2<<endl;   
cout.unsetf(ios::showpos);  //取消正數前加+號   
cout.setf(ios::showpoint);  //強制顯示小數點後的無效0   
cout<<f<< ' ' <<f1<< ' ' <<f2<<endl;   
cout.unsetf(ios::showpoint);  //取消顯示小數點後的無效0   
cout.setf(ios::scientific);  //科學記數法   
cout<<f<< ' ' <<f1<< ' ' <<f2<<endl;   
cout.unsetf(ios::scientific);  //取消科學記數法   
cout.setf(ios::fixed);  //按點輸出顯示   
cout<<f<< ' ' <<f1<< ' ' <<f2<<endl;   
cout.unsetf(ios::fixed);  //取消按點輸出顯示   
cout.precision(18);  //精度爲18,正常爲6   
cout<<f<< ' ' <<f1<< ' ' <<f2<<endl;   
cout.precision(6);  //精度恢復爲6   
}   
操縱算子:   
/*關於浮點數的格式*/  
#include <iomanip.h>   
void  main()   
{   
float  f=2.0/3.0,f1=0.000000001,f2=-9.9;   
cout<<f<< ' ' <<f1<< ' ' <<f2<<endl;  //正常輸出   
cout<<setiosflags(ios::showpos);  //強制在正數前加+號   
cout<<f<< ' ' <<f1<< ' ' <<f2<<endl;   
cout<<resetiosflags(ios::showpos);  //取消正數前加+號   
cout<<setiosflags(ios::showpoint);  //強制顯示小數點後的無效0   
cout<<f<< ' ' <<f1<< ' ' <<f2<<endl;   
cout<<resetiosflags(ios::showpoint);  //取消顯示小數點後的無效0   
cout<<setiosflags(ios::scientific);  //科學記數法   
cout<<f<< ' ' <<f1<< ' ' <<f2<<endl;   
cout<<resetiosflags(ios::scientific);  //取消科學記數法   
cout<<setiosflags(ios::fixed);  //按點輸出顯示   
cout<<f<< ' ' <<f1<< ' ' <<f2<<endl;   
cout<<resetiosflags(ios::fixed);  //取消按點輸出顯示   
cout<<setprecision(18);  //精度爲18,正常爲6   
cout<<f<< ' ' <<f1<< ' ' <<f2<<endl;   
cout<<setprecision(6);  //精度恢復爲6   
}  
相關文章
相關標籤/搜索