获得cout的精确度

klm*_*123 2 c++ precision cout

我可以使用设置cout输出的精度

cout.precision(precision_value);
Run Code Online (Sandbox Code Playgroud)

如何获得cout已经使用的精度值?我在cout 描述中找不到任何其他相关功能.

我遇到的相关问题是如何仅为代码的某些部分更改cout精度(因此需要在之后重置为精确的先前值).

我试过:

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

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

 // code

cout.flags(coutFlags);
Run Code Online (Sandbox Code Playgroud)

但这不起作用.

jro*_*rok 7

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

要保存并稍后将其设置回来:

auto old_precision = cout.precision(temp_precision);
// do stuff
cout.precision(old_precision);
Run Code Online (Sandbox Code Playgroud)