C++入门经典-例2.7-控制cout打印格式程序

1:代码如下:

// 2.7.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <iostream>
#include <iomanip>
using namespace std;
void main()
{
    double a=123.456789012345;
    cout << a << endl;//默认输出格式(精度为6)  
    cout << setprecision(9) << a << endl;//设置浮点数的输出精度为9,取9位有效数字
    cout << setprecision(6);    //恢复默认格式(精度为6)
    cout << setiosflags(ios::fixed); 
    cout << setiosflags(ios::fixed) << setprecision(8) << a << endl;//取小数点后8位有效数字
    cout << setiosflags(ios::scientific) << a << endl;
    cout << setiosflags(ios::scientific) << setprecision(4) << a << endl; 
}
View Code

运行结果:

2:程序中有几个语句不太明白。

posted @ 2017-09-11 10:03  一串字符串  阅读(305)  评论(0编辑  收藏  举报