How to retain 2 decimal places in C + +.
Cout<<setiosflags (ios::fixed) <<setprecision (2) << variable <<endl;
The above code requires a header file #include <iomanip> .
The Setprecision function refers to the precision of setting the output, for example:
float a=2017.666;
Cout<<setprecision (5) <<a;
2017.7** will be output (note: This will be rounded) * * If the parameter in the setprecision is less than the integer digits, it will be exported exponentially, for example:
float a=123.666;
Cout<<setprecision (2) <<a; Will output 1.2e+002. Setiosflags (ios::fixed) refers to a fixed floating-point display, when the Setiosflags (ios::fixed) and Setprecision (n) two are used together to identify the reserved n for fractional output. It's also important to note that each output is set sequentially, because the two-part scope is the successor, not the one that works on the last object.