C++格式化输出(举例)
来源:互联网 发布:黑客篡改时时彩数据 编辑:程序博客网 时间:2024/06/05 05:22
详细实现如下:/*关于浮点数的格式*/#include <iostream>#include <iomanip>using namespace std;int main(){ float f=3.0/7.0,f1=0.123456789,f2=-9.9; //正常输出 cout<<"正常输出: "<<f<<' '<<f1<<' '<<f2<<endl; //强制显示小数点后的无效0 cout<<setiosflags(ios::showpoint); cout<<"强制显示小数点后的无效0: "<<f<<' '<<f1<<' '<<f2<<endl; //取消显示小数点后的无效0 cout<<resetiosflags(ios::showpoint); //科学记数法 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); //精度为8,正常为6 cout<<setprecision(8); cout<<"精度为8显示: "<<f<<' '<<f1<<' '<<f2<<endl; //精度恢复为6 cout<<setprecision(6); //小数点后3位输出 cout<<setiosflags(ios::fixed); cout<<setprecision(3)<<"小数点后3位输出: "<<f<<' '<<f1<<' '<<f2<<endl; //精度恢复为6 cout<<setprecision(6);//宽度对齐输出 cout<<"宽度对齐输出: "<<setw(10)<<f<<' '<<f1<<' '<<f2<<endl; return 0;}