setprecision()与setiosflags() (转载http://blog.csdn.net/yunhaiC/archive/2008/03/11/2167462.aspx)
2009-10-27 09:24
525 查看
使用setprecision(n)可控制输出流显示浮点数的数字个数。C++默认的流输出数值有效位是6。
如果setprecision(n)与setiosflags(ios::fixed)合用,可以控制小数点右边的数字个数。setiosflags(ios::fixed)是用定点方式表示实数。
如果与setiosnags(ios::scientific)合用, 可以控制指数表示法的小数位数。setiosflags(ios::scientific)是用指数方式表示实数。
例如,下面的代码分别用浮点、定点和指数方式表示一个实数:
//*********************
//** ch2_1.cpp **
//*********************
#include <iostream.h>
#include <iomanip.h> //要用到格式控制符
void main()
{
double amount = 22.0/7;
cout < <amount < <endl;
cout < <setprecision(0) < <amount < <endl
< <setprecision(1) < <amount < <endl
< <setprecision(2) < <amount < <endl
< <setprecision(3) < <amount < <endl
< <setprecision(4) < <amount < <endl;
cout < <setiosflags(ios::fixed);
cout < <setprecision(8) < <amount < <endl;
cout < <setiosflags(ios::scientific)
< <amount < <endl;
cout < <setprecision(6); //重新设置成原默认设置
}
运行结果为:
3.14286
3
3
3.1
3.14
3.143
3.14285714
3.14285714e+00
该程序在32位机器上运行通过。
在用浮点表示的输出中,setprecision(n)表示有效位数。
第1行输出数值之前没有设置有效位数,所以用流的有效位数默认设置值6:第2个输出设置了有效位数0,C++最小的有效位数为1,所以作为有效位数设置为1来看待:第3~6行输出按设置的有效位数输出。
在用定点表示的输出中,setprecision(n)表示小数位数。
第7行输出是与setiosflags(ios::fixed)合用。所以setprecision(8)设置的是小数点后面的位数,而非全部数字个数。
在用指数形式输出时,setprecision(n)表示小数位数。
第8行输出用setiosflags(ios::scientific)来表示指数表示的输出形式。其有效位数沿用上次的设置值8
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/yunhaiC/archive/2008/03/11/2167462.aspx
如果setprecision(n)与setiosflags(ios::fixed)合用,可以控制小数点右边的数字个数。setiosflags(ios::fixed)是用定点方式表示实数。
如果与setiosnags(ios::scientific)合用, 可以控制指数表示法的小数位数。setiosflags(ios::scientific)是用指数方式表示实数。
例如,下面的代码分别用浮点、定点和指数方式表示一个实数:
//*********************
//** ch2_1.cpp **
//*********************
#include <iostream.h>
#include <iomanip.h> //要用到格式控制符
void main()
{
double amount = 22.0/7;
cout < <amount < <endl;
cout < <setprecision(0) < <amount < <endl
< <setprecision(1) < <amount < <endl
< <setprecision(2) < <amount < <endl
< <setprecision(3) < <amount < <endl
< <setprecision(4) < <amount < <endl;
cout < <setiosflags(ios::fixed);
cout < <setprecision(8) < <amount < <endl;
cout < <setiosflags(ios::scientific)
< <amount < <endl;
cout < <setprecision(6); //重新设置成原默认设置
}
运行结果为:
3.14286
3
3
3.1
3.14
3.143
3.14285714
3.14285714e+00
该程序在32位机器上运行通过。
在用浮点表示的输出中,setprecision(n)表示有效位数。
第1行输出数值之前没有设置有效位数,所以用流的有效位数默认设置值6:第2个输出设置了有效位数0,C++最小的有效位数为1,所以作为有效位数设置为1来看待:第3~6行输出按设置的有效位数输出。
在用定点表示的输出中,setprecision(n)表示小数位数。
第7行输出是与setiosflags(ios::fixed)合用。所以setprecision(8)设置的是小数点后面的位数,而非全部数字个数。
在用指数形式输出时,setprecision(n)表示小数位数。
第8行输出用setiosflags(ios::scientific)来表示指数表示的输出形式。其有效位数沿用上次的设置值8
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/yunhaiC/archive/2008/03/11/2167462.aspx
相关文章推荐
- 程序员是这样炼成的(13)-学会“抄袭” 收藏 (转载http://blog.csdn.net/GODYAD/archive/2011/04/17/6329925.aspx)
- 动态连接库入门(转载自光头的学习文档http://blog.csdn.net/yuguanglou/archive/2004/11/10/175879.aspx)
- Sql Server 生成连续日期的方法总结 本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/GRANDTREE/archive/2009/06/16/4273129.aspx
- android 中用Intent提取缩略图和原始图像(转载http://blog.csdn.net/serverside/archive/2010/05/28/5631480.aspx)
- 预防SQL注入攻击之我见[转载http://blog.csdn.net/jyk/archive/2009/11/26/4882097.aspx]
- 使用 CL 编译器选项查看 C++ 类内存布局 (转载)http://blog.csdn.net/zhangcunli/archive/2009/10/23/4720781.aspx
- 创建一个filter实例(transform filter)【转载http://blog.csdn.net/captain_x/archive/2007/08/10/1737619.aspx】
- 转载两篇文章之一(35 岁前程序员要规划好的四件事)原文:http://blog.csdn.net/oiio/archive/2007/02/12/1508001.aspx
- 学习UpdatePanel控件-看原文有图,本文只为个人备忘(转载:http://blog.csdn.net/ILOVEMSDN/archive/2007/11/11/1879343.aspx)
- c#读取并修改App.config文件实例(转载:http://blog.csdn.net/abuhome/archive/2010/01/13/5184467.aspx)
- GDI中的坐标映射问题 (转载自http://blog.csdn.net/huawdai/archive/2002/01/15/6169.aspx)
- 转载两篇文章之二(从程序员到CTO所要培养的六种能力)作者:阿蒙 原文:http://blog.csdn.net/harrymeng/archive/2007/02/07/1503931.aspx
- WSDL: 描述你的Web服务(转载http://blog.csdn.net/flyingBox/archive/2006/01/01/568162.aspx)
- form数据传递 转载http://blog.csdn.net/lxxlql/archive/2009/08/04/4408158.aspx
- RAC的GCS和GES 转载自http://blog.csdn.net/csucxcc/archive/2010/09/02/5857373.asp
- 哈哈,收藏源代码(本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/hakule/archive/2008/09/01/2861688.aspx)
- Ext2.0框架的Grid使用介绍--转载http://blog.csdn.net/chiangbt/archive/2008/01/24/2064117.aspx
- 成功的背后!(给所有IT人)(转载来自http://blog.csdn.net/ysuncn/archive/2007/10/07/1814127.aspx)
- s3c2440a——startup.s[转载出处:http://blog.csdn.net/qiaohuiic/archive/2009/06/04/4242836.aspx]
- C++ 事件机制实现(转载自http://blog.csdn.net/khler/archive/2007/08/07/1729638.aspx)