setprecision、fixed、showpoint的用法总结
来源:互联网 发布:考勤机删除数据恢复 编辑:程序博客网 时间:2024/06/05 07:44
首先要加头文件:
iomanip一:setprecision
作用:控制输出流显示浮点数的数字个数,setprecision(n)就是输出的n个数,会有四舍五入。
比如:double s=20.7843000,
cout<<setprecision(1)<<s<<endl;会输出2e+001,因为要输出一个数字,所以只有2.
cout<<setprecision(2)<<s<<endl;会输出21。
cout<<setprecision(3)<<s<<endl;会输出20.8。
cout<<setprecision(6)<<s<<endl;会输出20.7843。
cout<<setprecision(7)<<s<<endl;会输出20.7843。
cout<<setprecision(8)<<s<<endl;会输出20.7843。
可见,小数部分末尾为0时,是输不出来的!
要想输出来,就得用showpoint了。
特别提示:
(如果再在这些语句后面加个两个语句:
cout<<1<<endl;
cout<<1.00800<<endl;
猜到会输出什么吗?
第一条输出:1。不是浮点型。
第二条为:1.008。承接setprecision(8)的这条规则语句。
注:
如果直接有语句
int main()
{
cout<<1<<endl;
cout<<1.00<<endl;
}
第一条输出:1。
第二条也为:1。按整型输出
)
二:setprecision与showpoint
语法:在输出语句前声明:cout.setf(ios::showpoint);就行了!
还比如:double s=20.7843000,
cout.setf(ios::showpoint);
cout<<setprecision(1)<<s<<endl;就会输出2.e+001,注意,2和e之间多了一个“.”。
cout<<setprecision(2)<<s<<endl;会输出21.。多个点!
cout<<setprecision(3)<<s<<endl;会输出20.8。
cout<<setprecision(6)<<s<<endl;会输出20.7843。
cout<<setprecision(7)<<s<<endl;会输出20.78430。
cout<<setprecision(8)<<s<<endl;会输出20.784300。
可见,就会输出想要的数据数目!
特别提示:
(如果再在这些语句后面加个两个语句:
cout<<1<<endl;
cout<<1.0080<<endl;
猜到会输出什么吗?
第一条输出:1。不是浮点型。
第二条也为:1.0080000。承接setprecision(8)的这条规则语句。
三:setprecision与fixed
如果想要保留几位小数,那setprecision就得与fixed合作了!!
语法:在输出语句前声明:cout.setf(ios::fixed);
比如:double s=20.7843909
cout.setf(ios::fixed);
cout<<setprecision(1)<<s<<endl;就会输出2.8 。
cout<<setprecision(2)<<s<<endl;会输出21.78。多个点!
cout<<setprecision(3)<<s<<endl;会输出20.784。
cout<<setprecision(6)<<s<<endl;会输出20.784391。
cout<<setprecision(7)<<s<<endl;会输出20.7843909。
cout<<setprecision(8)<<s<<endl;会输出20.78439090。
特别提示:
(如果也再在这些语句后面加个两个语句:
cout<<1<<endl;
cout<<1.008<<endl;
猜到会输出什么吗?
第一条输出:1。
第二条为:1.00800000。
就是承接了setprecision(8)的这条规则语句,是浮点型的都会保留8个小数。是整型的还是整型!)
语句也可以写成:cout<<fixed<<setprecision(2)<<s<<endl;
就算后面的语句没有写<<fixed,同样会按有<<fixed处理。
比如有语句:
cout<<fixed<<setprecision(2)<<s<<endl;
A:cout<<setprecision(7)<<s<<endl;
B:cout<<setprecision(8)<<s<<endl;
AB语句均会按保留7个,8个小数处理,不会再按有7或8个浮点数处理。
如果下面有语句c:
cout<<1.008<<endl;也会保留8个小数。
四:setprecision、showpoint与fixed
{cout<<fixed<<setprecision(2)<<123.456<<endl;//输出的结果是123.46
cout<<showpoint<<12345.0006666<<endl;//输出12345.0
cout<<fixed<<setprecision(2)<<123.456<<endl;}
比如:double s=20.7843909
1.有语句
cout<<setprecision(2)<<s<<endl;//输出21
cout<<fixed<<s<<endl;//输出20.78
2.有语句:
cout<<setprecision(2)<<s<<endl;//输出21
cout<<showpoint<<s<<endl;//输出21.(有个点)
3.有语句:
cout<<fixed<<s<<endl;//输出20.78391
cout<<showpoint<<s<<endl;//输出20.78391
4.有语句:
cout<<setprecision(2)<<s<<endl;//输出21
cout<<fixed<<s<<endl;//输出20.78
cout<<showpoint<<s<<endl;//输出20.78
5.有语句:
cout<<setprecision(2)<<s<<endl;//输出21
cout<<showpoint<<s<<endl;//21.(有个点)
cout<<fixed<<s<<endl;//20.78
- setprecision、fixed、showpoint的用法总结
- setprecision、fixed、showpoint的用法总结
- setprecision、fixed、showpoint的用法总结
- setprecision、fixed、showpoint的用法总结
- setprecision、fixed、showpoint的用法总结(经典!!超经典!!)
- setprecision、fixed、showpoint的用法总结(经典!!超经典!!)
- setprecision、fixed、showpoint的用法总结(经典!!超经典!!)
- setprecision、fixed、showpoint的用法总结(经典!!超经典!!)
- cout<< 精度控制 setprecision、fixed、showpoint的用法总结
- setprecision、fixed、showpoint的用法总结 (经典!!超经典!!)
- fixed 和setprecision()的用法
- fixed 和setprecision()的用法
- C++随笔 —— std::setprecision与showpoint和ios::fixed的配合使用
- 【资料整理】fixed()和setprecision()函数的用法
- fixed 和setprecision()的用法(c++中)
- fixed和setprecision的使用
- fixed<<setprecision(n)
- fixed和setprecision等C++格式控制的函数使用
- c++导出类示例
- linux常用命令
- COM入门(1)
- 数模随想
- 修改测试apk读出的CPU主频
- setprecision、fixed、showpoint的用法总结
- COM入门(2)
- cellForRowAtIndexPath出错原因分析
- php mail
- Linux修改系统时间
- COM入门(3)
- extern “C”总结
- Linux下怎么修改mac地址?
- Servlet性能优化