setting precision

i've declared my variables as doubles, yet when i try and set the precision of the percentage difference the numbers come back as, e.g. -3.2+001 - and it also messes up the other variables in their output

does anyone know the solution or how i would set the precision of the percentage difference to one decimal place?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
  x=0;
	for (x>=0; x<=100; x++)
	{
		approx = (x*2)+30;
		actual = (x*9.5)+32;
		diff = approx-actual;
		pdiff = (((approx-actual)/approx)*100);

	
		cout << x << "\t" << approx << "\t" << actual << "\t" << diff << "\t" << "\t"  << setprecision(1) <<pdiff << "\n";
	}

	getch();
	return(0);
	

}
Sounds like you want to use std::fixed.
http://www.cplusplus.com/reference/ios/fixed/
as in...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
x=0;
	for (x>=0; x<=100; x++)
	{
		approx = (x*2)+30;
		actual = (x*9.5)+32;
		diff = approx-actual;
		pdiff = (((approx-actual)/approx)*100);


		cout << x << "\t" << approx << "\t" << actual << "\t" << diff << "\t" << "\t"  << std::setprecision(2)<< pdiff << "\n";
	}

	getch();
	return(0);
	

}


because thats not working? sorry i'm brand new to c++
No I mean you should use std::fixed similar to how you have used std::setprecision.
std::cout << std::fixed << std::setprecision(1) << 1.2345; // prints "1.2"
Topic archived. No new replies allowed.