温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

c++输出精度控制

发布时间:2020-07-15 10:56:15 来源:网络 阅读:4294 作者:星星流浪 栏目:移动开发

1.setprecision(n);

    默认设置输出的数字的总位数为n,包含整数和小数部分。

2.setiosflags(ios::fixed)

    默认输出6位,必须与setprecision(n)配合使用,用来控制小数位数,不够补0

3.resetiosflags(ios::fixed);

    取消精度的设置
 

  1. #include <iostream> 
  2. #include <iomanip> 
  3. using namespace std; 
  4. int main () { 
  5.     double f =3.14159; 
  6.     cout << setprecision (5) << f << endl;                //3.1416        
  7.     cout << setprecision (9) << f << endl;                 //3.14159 
  8.     cout << fixed<<setprecision (5) << f << endl;    //3.14159 
  9.     cout <<fixed<< setprecision (9) << f << endl;    //3.141590000 
  10.     cout<<f<<endl;                                                  //3.141590000 
  11.     cout<<resetiosflags(ios::fixed)<<setprecision(9)<<f;//3.14159 
  12.     return 0; 
  13. }  


需注意在精度的设置是全局作用,
setiosflags之后要resetiosflags还原,位数也是如此!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI