您的位置:首页 > 编程语言 > C语言/C++

C++ 保留几位小数

2016-04-19 17:19 447 查看
#include<iostream>

#include<iomanip>   //头文件不可少

using namespace std;

int main()

{
      double a = 3.14159;
      cout << setiosflags(ios::fixed) << setprecision(0) << a << endl;  //保留0位小数
      cout << setiosflags(ios::fixed) << setprecision(1) << a << endl;  //保留1位小数
      cout << setiosflags(ios::fixed) << setprecision(2) << a << endl;  //保留2位小数
      cout << setiosflags(ios::fixed) << setprecision(3) << a << endl;  //保留3位小数,不仅仅截断字符,还可以四舍五入
      cout << setiosflags(ios::fixed) << setprecision(4) << a << endl;  //保留4位小数
      cout << setiosflags(ios::fixed) << setprecision(5) << a << endl;  //保留5位小数
      cout << setiosflags(ios::fixed) << setprecision(6) << a << endl;  //保留6位小数
      return 0;

}

输出:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: