/*关于浮点数的格式*/
#include <iostream.h>
void main()
{
float f=2.0/3.0,f1=0.000000001,f2=-9.9;
cout<<f<<' '<<f1<<' '<<f2<<endl; //正常输出
cout.setf(ios::showpos); //强制在正数前加+号
cout<<f<<' '<<f1<<' '<<f2<<endl;
cout.unsetf(ios::showpos); //取消正数前加+号
cout.setf(ios::showpoint); //强制显示小数点后的无效0
cout<<f<<' '<<f1<<' '<<f2<<endl;
cout.unsetf(ios::showpoint); //取消显示小数点后的无效0
cout.setf(ios::scientific); //科学记数法
cout<<f<<' '<<f1<<' '<<f2<<endl;
cout.unsetf(ios::scientific); //取消科学记数法
cout.setf(ios::fixed); //按点输出显示
cout<<f<<' '<<f1<<' '<<f2<<endl;
cout.unsetf(ios::fixed); //取消按点输出显示
cout.precision(18); //精度为18,正常为6
cout<<f<<' '<<f1<<' '<<f2<<endl;
cout.precision(6); //精度恢复为6
}
操纵算子:
/*关于浮点数的格式*/
#include <iomanip.h>
void main()
{
float f=2.0/3.0,f1=0.000000001,f2=-9.9;
cout<<f<<' '<<f1<<' '<<f2<<endl; //正常输出
cout<<setiosflags(ios::showpos); //强制在正数前加+号
cout<<f<<' '<<f1<<' '<<f2<<endl;
cout<<resetiosflags(ios::showpos); //取消正数前加+号
cout<<setiosflags(ios::showpoint); //强制显示小数点后的无效0
cout<<f<<' '<<f1<<' '<<f2<<endl;
cout<<resetiosflags(ios::showpoint); //取消显示小数点后的无效0
cout<<setiosflags(ios::scientific); //科学记数法
cout<<f<<' '<<f1<<' '<<f2<<endl;
cout<<resetiosflags(ios::scientific); //取消科学记数法
cout<<setiosflags(ios::fixed); //按点输出显示
cout<<f<<' '<<f1<<' '<<f2<<endl;
cout<<resetiosflags(ios::fixed); //取消按点输出显示
cout<<setprecision(18); //精度为18,正常为6
cout<<f<<' '<<f1<<' '<<f2<<endl;
cout<<setprecision(6); //精度恢复为6
}
【C++】cout格式控制
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
推荐阅读更多精彩内容
- 1.函数基本说明:函数名: scanf功 能: 执行格式化输入用 法: int scanf(char *forma...
- 1 C++输出 先介绍一个不太好用的,基于cout的格式控制: 常见的是保留几位小数的问题e.g. 保留7位小数#...
- .net中,处于安全的考虑,RSACryptoServiceProvider类,解密时只有同时拥有公钥和私钥才可以...
- 成长记录-连载(三十六) ——我的第一篇五千字长文,说了什么,你一定想不到 并不是不想每天写公众号,而是之前思考怎...