C++输出精度控制

2014-11-24 08:47:16 来源: 作者: 浏览: 0

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<cout <10.cout<cout<return 0;
}


需注意在精度的设置是全局作用,setiosflags之后要resetiosflags还原,位数也是如此!


-->

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: