Indeed, what is the opposite of a “fixed” I / O manipulator?

This may be a duplicate of this question , but I don’t feel that I really answered it correctly. Note:

#include <iostream> #include <iomanip> using namespace std; int main () { float p = 1.00; cout << showpoint << setprecision(3) << p << endl; } 

Output: 1.00

Now, if we change this line to:

  cout << fixed << showpoint << setprecision(3) << p << endl; 

we get: 1.000

And if we use the “opposite” fixed, we get something completely different:

  cout << scientific << showpoint << setprecision(3) << p << endl; 

Output: 1.000e+00

How can I return to the behavior of the first version after installing fixed ?

+4
source share
3 answers

The format specification for floating points is the bitmax call std::ios_base::floatfield . In C ++ 03, it has two named settings ( std::ios_base::fixed and std::ios_base::scientific ). By default, none of these flags are set. This can be achieved, for example, using

 stream.setf(std::ios_base::fmtflags(), std::ios_base::floatfield); 

or

 stream.unsetf(std::ios_base::floatfield); 

(field type std::ios_base::fmtflags ).

With current C ++, there is also std::ios_base::hexfloat and two manipulators have been added, in particular std::defaultfloat() , which clears std::ios_base::floatfield :

 stream << std::defaultfloat; 
+6
source

I think the answer is std::defaultfloat . However, this is only available in C ++ 11. See http://en.cppreference.com/w/cpp/io/manip/fixed .

+3
source

Before C ++ 11, you can clear the fixed flag, but not with the manipulator:

 #include <iostream> #include <iomanip> using namespace std; int main() { float p = 1.00; cout << showpoint << fixed << setprecision(3) << p << endl; // change back to default: cout.setf(0, ios::fixed); cout << showpoint << setprecision(3) << p << endl; } 
0
source

Source: https://habr.com/ru/post/897345/


All Articles