第10章输入/输出流 例10.4控制浮点数精度 #inc!ude< iostream. h>通过 precision成员函数设置精 # include< omani. h>度后,将以前精度值返回并保存 void main( 在变量 Preprecision!中。 注意:系统默认精度为6, double value =31. 4142743. int Preprecision cout precision (4) cout < value < end: cout≤≤ setprecision( Preprecision)<<vaue≤<endl; 恢复原来的精度设置 程序运行结果为: 31.41 31.4143
例10.4 控制浮点数精度 # include <iostream.h> # include <iomanip.h> void main( ) { double value = 31.4142743; int Preprecision = cout.precision(4); cout << value << endl; cout << setprecision( Preprecision ) << value << endl; } 程序运行结果为: 31.41 31.4143 第10章 输入/输出流 通过precision成员函数设置精 度后,将以前精度值返回并保存 在变量Preprecision中。 注意:系统默认精度为6, 恢复原来的精度设置
第10章输入/输出流 「10.3格式化输入输出 10.33输出精度控制(续) 所设置的精度值,在程序没有设置计数法情况下,表示浮点数 的有效数字的个数。若程序设置了计数法(ios:fxed或 ios: scientific),则表示小数点后数字的个数。 ios:fied表示以定点法输出浮点数(不带指数)。 os: scientific表示以科学计数法输出浮点数
10.3 格式化输入输出 10.3.3 输出精度控制(续) 所设置的精度值,在程序没有设置计数法情况下,表示浮点数 的有效数字的个数 。 若程序 设置了计数法 ( ios::fixed 或 ios::scientific),则表示小数点后数字的个数。 ios::fixed 表示以定点法输出浮点数(不带指数)。 ios::scientific 表示以科学计数法输出浮点数。 第10章 输入/输出流