How to cout a float number with n decimal places [duplicate]

Solution 1:

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

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

These require following header:

#include <iomanip>

Solution 2:

Try setprecision:

cout.setf(ios::fixed);
cout << setprecision(3) << a << endl;