Examples
double x = 0.012574;
cout << x << endl; // outputs "0.012574"
cout << setprecision(4) << x << endl; // outputs "0.01257"
cout << fixed << x << endl; // outputs "0.0126"
cout << scientific << x << endl; // outputs "1.2574e-02"
Explanation
fixed
specifies that 'fixed' (non-scientific) notation should be used for all subsequent output of floating-point values
scientific
specifies scientific notation for subsequent output of floating-point values, such that 0.01 outputs as 1.000000e-2, etc
setprecision
sets precision for output of floating-point values
- By default, precision is maximum number of significant figures to use
- If fixed mode has been enabled, precision is the number of decimal places
- If scientific mode has been enabled, precision is the number of decimal places used for the significand