加载中…
个人资料
NSFish
NSFish
  • 博客等级:
  • 博客积分:0
  • 博客访问:474
  • 关注人气:8
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

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

(2011-12-09 14:57:44)
标签:

小数点

定点数

位数

科学计数法

浮点数

杂谈

分类: 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.
即当输出的数据类型为浮点型时,setprecision(int)表示设置输出数的有效位数,当输出的是定点数或用科学计数法表示的数时,setprecison(int)表示设置输出数小数点后的位数。
故通常控制浮点数输出时小数点后的位数的做法为:先将要输出的数设置为定点数:
fixed
再用setprecison设置小数点后位数:
setprecision(2)
Example:

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有