【转】setprecision、fixed、showpoint的用法总结(经典!!超经典!!)

原文链接:http://www.tuicool.com/articles/zie6Fz7#0-tsina-1-80285-397232819ff9a47a7b7e80a40613cfe1

 

首先要加头文件: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    

posted on 2015-05-08 16:22  SarahQQ  阅读(533)  评论(0编辑  收藏  举报

导航