cout的输出格式初探3
生活随笔
收集整理的這篇文章主要介紹了
cout的输出格式初探3
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
#include <iostream>
#include <iomanip>
using namespace std;int main()
{double 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); //強制顯示小數點后的無效0cout<<f<<' '<<f1<<' '<<f2<<endl;cout.unsetf(ios::showpoint); //取消顯示小數點后的無效0cout.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,正常為6cout<<f<<' '<<f1<<' '<<f2<<endl;cout.precision(6); //精度恢復為6cout<<f<<' '<<f1<<' '<<f2<<endl;cout<<"----------------------------"<<endl;//使用操作算法,效果相同cout<<f<<' '<<f1<<' '<<f2<<endl; //正常輸出cout<<setiosflags(ios::showpos); //強制在正數前加+號cout<<f<<' '<<f1<<' '<<f2<<endl;cout<<resetiosflags(ios::showpos); //取消正數前加+號cout<<setiosflags(ios::showpoint); //強制顯示小數點后的無效0cout<<f<<' '<<f1<<' '<<f2<<endl;cout<<resetiosflags(ios::showpoint); //取消顯示小數點后的無效0cout<<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,正常為6cout<<f<<' '<<f1<<' '<<f2<<endl;cout<<setprecision(6); //精度恢復為6cout<<f<<' '<<f1<<' '<<f2<<endl; return 0;
}
程序運行結果如下圖:
總結
以上是生活随笔為你收集整理的cout的输出格式初探3的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: cout的输出格式初探2
- 下一篇: C++命名空间namespace