输出的精度设置
streamsize prec=cout.precision(3); cout.precision(3)是把精度设成3么?那prec不也变成3了?cout << sum <<endl;
cout.precision(prec); 如果prec已经变成3了,那么cout.precision(prec);不是不能把精度设回默认值?
求详解
2012-07-08 10:01
2012-07-08 11:03
程序代码:01.#include <iostream>
02.#include <iomanip>
03.using namespace std;
04.
05.int main( void )
06.{
07. const double value = 12.3456789;
08.
09. cout << value << endl; // 默认以6精度,所以输出为 12.3457
10. cout << setprecision(4) << value << endl; // 改成4精度,所以输出为12.35
11. cout << setprecision(8) << value << endl; // 改成8精度,所以输出为12.345679
12. cout << fixed << setprecision(4) << value << endl; // 加了fixed意味着是固定点方式显示,所以这里的精度指的是小数位,输出为12.3457
13. cout << value << endl; // fixed和setprecision的作用还在,依然显示12.3457
14. cout.unsetf( ios::fixed ); // 去掉了fixed,所以精度恢复成整个数值的有效位数,显示为12.35
15. cout << value << endl;
16. cout.precision( 6 ); // 恢复成原来的样子,输出为12.3457
17. cout << value << endl;
18. cout<<fixed<<value<<endl;
19.}


2012-07-08 12:53