码迷,mamicode.com
首页 > 编程语言 > 详细

c++ cout 保留小数点位

时间:2015-09-17 23:08:38      阅读:416      评论:0      收藏:0      [点我收藏+]

标签:

需要头文件 <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

  

c++ cout 保留小数点位

标签:

原文地址:http://www.cnblogs.com/Emerald/p/4817813.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!