c++浮點數值輸出
阿新 • • 發佈:2019-02-16
#include<iostream> #include<iomanip> using namespace std; int main() { cout<<2.226215151515<<endl; cout<<"------------------------"<<endl; double a=1.568922156562; cout<<a<<endl; //預設有效位數是6 cout<<setprecision(0)<<"setprecision(0): "<<a<<endl <<setprecision(1)<<"setprecision(1): "<<a<<endl <<setprecision(2)<<"setprecision(2): "<<a<<endl <<setprecision(3)<<"setprecision(3): "<<a<<endl <<setprecision(4)<<"setprecision(4): "<<a<<endl; cout<<"------------------------"<<endl; //b的整數有三位,輸出顯示小數精度是兩位,所以系統自動轉化為指數輸出 double b=223.0226262; cout<<setprecision(2)<<"輸出指數: "<<b<<endl; cout<<"------------------------"<<endl; //設定固定浮點數 cout<<setiosflags(ios::fixed); cout<<setprecision(8)<<"setprecision(8): "<<a<<endl; cout<<"------------------------"<<endl; cout.unsetf(ostream::floatfield); //恢復設定 cout<<setiosflags(ios::scientific)<<"指數格式: "<<a<<endl; cout<<std::fixed; //清除使用指數格式 cout<<"------------------------"<<endl; cout<<setprecision(6); //沒有清除前面的固定浮點數輸出,之後還是輸出固定浮點數 cout<<2.226215151515<<endl; cout<<2.1515<<endl; cout<<"------------------------"<<endl; cout.unsetf(ostream::floatfield); //使用恢復設定 cout<<setprecision(6); cout<<2.226215151515<<endl; cout<<2.1515<<endl; } <img src="https://img-blog.csdn.net/20161031195424654?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" align="left" alt="" />