printing whole numbers as double

I am trying to print a whole number as a double, using set precision and it does not work. It works for double number but not for whole numbers.

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

int main()
{
  double val = 2.0;
  std::cout << setprecision(3)<< val << endl;
  return 0;
}
Last edited on
You should use fixed

Try doing this:

 
std::cout<<setprecision(3)<<fixed<<val<<endl;
Thanks a lot that worked :)
Topic archived. No new replies allowed.