c++浮点数值输出

#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="" />

 

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值