c++浮点数值输出

来源:互联网 发布:php 反转字符串 编辑:程序博客网 时间:2024/05/16 12:13
#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="http://img.blog.csdn.net/20161031195424654?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" align="left" alt="" />


1 0