C++中控制浮点数输出时小数点后的位数

标签:
小数点定点数位数科学计数法浮点数杂谈 |
分类: Cpp |
关于setprecision(int),MSDN上的解释为:
If the display format is scientific or fixed, then the precision indicates the number of digits after the decimal point. If the format is automatic (neither floating point nor fixed), then the precision indicates the total number of significant digits. This setting remains in effect until the next change.
If the display format is scientific or fixed, then the precision indicates the number of digits after the decimal point. If the format is automatic (neither floating point nor fixed), then the precision indicates the total number of significant digits. This setting remains in effect until the next change.
即当输出的数据类型为浮点型时,setprecision(int)表示设置输出数的有效位数,当输出的是定点数或用科学计数法表示的数时,setprecison(int)表示设置输出数小数点后的位数。
故通常控制浮点数输出时小数点后的位数的做法为:先将要输出的数设置为定点数:
fixed
再用setprecison设置小数点后位数:
setprecision(2)
Example:
前一篇:【转】深入了解C宏
后一篇:Content Provider