double d = 1/2.;
printf("%.2lf\n", d);
This prints out 0.50. This is what I want to replicate using ostream manipulators. However, none of the obvious iomanip manipulators let me set the minimum required decimal places (if I understood correctly, setprecision sets the maximum width). Is there a pure iostream or boost way to do this?
Use
setprecisionin combination withfixed.According to section 22.4.2.2.2 of the standard, precision specifications on iostreams have exactly the same effect as they do for
printf. Andfixedgives the exact same behavior asprintf‘s%f.