C++输出精度控制
2014 年 7 月 21 日
<
div id=”content” contentScore=”784″>1.setprecision(n);
默认设置输出的数字的总位数为n,包含整数和小数部分。
2.setiosflags(ios::fixed)
默认输出6位,必须与setprecision(n)配合使用,用来控制小数位数,不够补0
3.resetiosflags(ios::fixed);
取消精度的设置
#include
#include
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<
10.cout<<f<<endl; //3.141590000
cout<<resetiosflags(ios::fixed)<<setpre