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

c++输出精度控制

2013-01-07 22:11 363 查看
1.setprecision(n);

默认设置输出的数字的总位数为n,包含整数和小数部分。

2.setiosflags(ios::fixed)

默认输出6位,必须与setprecision(n)配合使用,用来控制小数位数,不够补0

3.resetiosflags(ios::fixed);

取消精度的设置

#include <iostream>
#include <iomanip>
using namespace std;
int main () {
double f =3.14159;
cout << setprecision (5) << f << endl;                //3.1416
cout << setprecision (9) << f << endl;                 //3.14159
cout << fixed<<setprecision (5) << f << endl;    //3.14159
cout <<fixed<< setprecision (9) << f << endl;    //3.141590000
cout<<f<<endl;                                                  //3.141590000
cout<<resetiosflags(ios::fixed)<<setprecision(9)<<f;//3.14159
return 0;
}


需注意在精度的设置是全局作用,setiosflags之后要resetiosflags还原,位数也是如此!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c++ 输出精度控制