您的位置:首页 > 编程语言 > PHP开发

setprecision、fixed、showpoint的用法总结

2016-01-21 12:20 766 查看
转自:http://blog.csdn.net/u011321546/article/details/9293547

-------------------------------------------------------------------------------------------------------------------
首先要加头文件:#include <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
设定为showpoint后,在不必要的时候也显示10进数的小数点以及其后的0。

语法:在输出语句前声明: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<<showpoint<<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<<fixed<<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;//输出123.46
}
也可这样写:cout<<fixed<<showpoint<<setprecision(2)<<12.096<<endl;  //输出12.10

比如: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

例程参考用法:
#include <iostream>
#include <iomanip>
using namespace std;
int main( void )
{
const double value = 12.3456789;
cout << value << endl; // 默认以6精度,所以输出为 12.3457
cout << setprecision(4) << value << endl; // 改成4精度,所以输出为12.35
cout << setprecision(8) << value << endl; // 改成8精度,所以输出为12.345679
cout << fixed << setprecision(4) << value << endl; // 加了fixed意味着是固定点方式显示,所以这里的精度指的是小数位,输出为12.3457
cout << value << endl; // fixed和setprecision的作用还在,依然显示12.3457
cout.unsetf( ios::fixed ); // 去掉了fixed,所以精度恢复成整个数值的有效位数(4位)而不是小数点后的4位,显示为12.35
cout << value << endl;
cout.precision( 6 ); // 恢复成原来的样子,输出为12.3457
cout << value << endl;
return 0;
  }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  C++