获得cout的精度 [英] Get precision of cout

查看:174
本文介绍了获得cout的精度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以使用

  cout设置 cout 精度(precision_value); 

如何获取精确值, c $ c> cout 已经?
我在 cout description



我遇到的相关问题是如何只改变代码的某些部分的cout精度

 <$ c $ 

c> // save original cout flags
std :: ios_base :: fmtflags coutFlags = cout.flags();

cout.precision(1);
cout.setf(ios :: fixed);

// code

cout.flags(coutFlags);

但这不行。

解决方案

此功能是重载 。两个重载都返回在调用函数之前设置的精度。 cout.precision()可能是你想查询的。



要保存并稍后再设置:

  auto old_precision = cout.precision(temp_precision); 
// do stuff
cout.precision(old_precision);


I can set precision of cout output using

cout.precision(precision_value);

how can I get the precision value, which is used by cout already? I can't find any other related function in cout description.

The related problem I faced is how can I change cout precision for some part of a code only (so it need to be reset afterwards to exact previous value).

I tryed:

//save original cout flags
std::ios_base::fmtflags coutFlags = cout.flags();

cout.precision(1);
cout.setf(ios::fixed);

 // code

cout.flags(coutFlags);

but this doens't work.

解决方案

The function is overloaded. Both overloads return the precision that was set before the call to the function. cout.precision() may be what you want to query it.

To save it and set it back later:

auto old_precision = cout.precision(temp_precision);
// do stuff
cout.precision(old_precision);

这篇关于获得cout的精度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆