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 tel
setw
setprecision
If you want the trailing zero from rounding, you can use the C function printf.
printf
#include #include int main() { float v = 12.3961; std::printf("%.2f",v); //prints 12.40 }
Compared to:
#include #include int main() { float v = 12.3961; std::cout << std::setprecision(4) << v; //prints 12.4 }