To get the same format specified in %6.3f using only standard iostream manipulators, you can do:
std::cout << std::fixed << std::setw(6) << std::setprecision(3) << f;
In particular, std::fixed indicates the same base format as f in the format string, so for example, "precision" means the same for the format string and for ostream. std::setprecision(3) then actually sets the precision, and std::setw(6) sets the field width. Without setting std::fixed you will get a format similar to the format specified in the format string "%6.3g" .
Note that besides setw these manipulators are sticky. That is, they remain valid after the release of one variable.
source share