How to cout a float number with n decimal places

Muhammad Barrima picture Muhammad Barrima · Feb 3, 2013 · Viewed 83.3k times · Source

Possible Duplicate:
How do I print a double value with full precision using cout?

float a = 175.;
   cout << a;

If I run the previous code I'll get just 175, how can I cout the number with (for example) 3 decimal places even they were zeros .. How can I print "175.000" ?!

Answer

Jesse Good picture Jesse Good · Feb 3, 2013

You need std::fixed and std::setprecision:

 std::cout << std::fixed << std::setprecision(3) << a;

These require following header:

#include <iomanip>