c++ cout 保留小数点位

需要头文件 <iomanip>

输出时需要用 fixed 和 setprecision()

fixed代表输出浮点数,setprecision()设置精度。

#include <iostream>
#include <iomanip>
#include <cstdio>

using namespace std;

int main(int argc, char const *argv[]) {
    printf("%.2lf\n", 12.345);
    cout <</* fixed << */setprecision(2) << 12.345 << endl;
    cout << fixed << setprecision(2) << 12.345 << endl;
    return 0;
}

输出结果为:

12.35
12
12.35

  

posted @ 2015-09-17 22:02  Emerald  阅读(1025)  评论(0编辑  收藏  举报