# 设置浮点数精度(precision、setprecision)

 // Fig. 11.17: fig11_17.cpp
// Controlling precision of floating-point values
#include < iostream.h>
#include < iomanip.h>
#include < math.h>
int main()
double root2 = sqrt( 2.0 );
int places;
cout << setiosflags(ios::fixed)
<< "Square root of 2 with precisions 0-9.\n"
<< "Precision set by the"
<< "precision member function:" << endl;
for ( places = 0; places <= 9; places++ ) {
cout.precision( places );
cout << root2 << '\n';
}
cout << "\nPrecision set by the"
<< "setprecision manipulator:\n";
for ( places = 0; places <= 9; places++ )
cout << setprecision( places ) << root2 << '\n';
return 0;
}


Square root of 2 with pzecisions 0-9.

Precision set by the precision member function:

1

1.4

1.41

1.414

1.4142

1.41421

1.414214

1.4142136

1.41421356

1.414213562

Precision set by the setprecision manipulator:

1

1.4

1.4l

1.414

1.4142

1.41421 ·

1.414214

1.4142136

1.41421356

1.414213562

FROM: http://blog.163.com/kevinlee_2010/blog/static/16982082020141287163589/

#### C++中控制浮点数精度函数setprecision使用方法

2013-05-12 21:41:28

#### 交叉GCC与浮点处理

2014-02-11 15:04:44

#### GCC对四精度浮点型的支持

2012-09-02 20:04:07

#### C++最简单的四舍五入！！（setprecision(n)方法)

2014-11-13 16:51:20

#### setprecision

2016-03-22 11:50:40

#### lua之数据类型

2014-02-18 16:23:13

#### C++中输出精度的设定

2017-03-16 19:33:19

#### 小数点输出精度控制问题

2009-03-29 17:32:00

#### cout.precision()的使用

2017-06-08 20:14:38

#### C++ precision() 函数

2014-09-05 11:10:48