C++输出保留有效数字

#include <iomanip>  
//保留小数点后2位
cout << setiosflags(ios::fixed) << setprecision(2) << val << endl;
//保留2位有效数字
cout << setprecision(2) << val << endl;
#include <iomanip>  
using namespace std;

int main()
{
    double val = 10.6;
    
    cout << setprecision(2) << val << endl;//保留两位有效数字
    cout << setiosflags(ios::fixed) << setprecision(2) << val << endl;//保留小数点后两位
    cout << resetiosflags(ios::fixed) << setprecision(2) << val << endl;//清除ios::fixed
    system("pause");
    return 0;
}

 

posted @ 2018-05-26 15:39  Zzz...y  阅读(5501)  评论(0编辑  收藏  举报