C++浮点数据的输出控制

#include <iostream>
#include <Windows.h>

using namespace std;

int main(void) {
    double value = 12.3456789;

    // cout默认输出6位有效数字
    cout << value << endl;

    // 修改cout的输出精度,设置成输出4位有效数字
    cout.precision(4);
    cout << value << endl;

    // 设置输出小数点后面的5位有效数字
    cout.precision(5);
    cout.flags(cout.fixed); //定点法,设置小数点后面输出的位数
    cout << value << endl;

    // 取消定点法
    cout.unsetf(cout.fixed);
    cout << value << endl;

    system("pause");
    return 0;
}

 输出结果

 

posted @ 2019-07-31 10:04  你爱过大海我爱过你  阅读(600)  评论(0编辑  收藏  举报