How do you format a float in C++ to output to two decimal places rounded up? I’m having no luck with setw and setprecision as my compiler just tells me they are not defined.
cout << "Total : " << setw(2) << total << endl;
total outputs: Total : 12.3961
I’d like it to be: 12.40 or 12.39 if it’s too much work to round up.
You need to include
<iomanip>and provide namespace scope tosetw and setprecisiontry:
or
iostream provides precision function, but to use setw, you may need to include extra header file.