Cout<<setiosflags (iOS::fixed) <<setprecision (2);// requires header file # include <iomanip>
Then output the real type variable that can retain 2 decimal places output, of course you want to keep three decimal places, Setprecision (3) on the line.
Setprecision refers to setting the output precision when there is no
Cout<<setiosflags (iOS::fixed)
, the output format is a valid number of digits for the data, such as
float 123.666; cout<<setprecision (5) <<a;
Will output 123.67 (note here that it will be rounded up). If the parameters in the setprecision are less than the integer digits, they will be output in exponential form, for example
float 123.666; cout<<setprecision (2) <<a;
Will output 1.2e+002.
Setiosflags (ios::fixed) refers to a fixed floating-point display that retains n-bit output when setiosflags (ios::fixed) and Serprecision (n) two. It is also important to note that each output is set only once, because the two scopes are subsequent objects, not just the latter.
How C + + preserves two-bit decimals and significant digits