Feb 12, 2010 at 4:51pm Feb 12, 2010 at 4:51pm UTC
It prints 345 with no decimal;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
#include <iostream>
using namespace std;
double placevalue (double a)
{
double b = a * 100;
int c = b;
double d = c / 100;
return (d);
}
int main () {
double x = 345.2359745;
double y;
y = placevalue (x);
cout << "#:" << y;
cin.get();
return 0;
}
#:345
Last edited on Feb 12, 2010 at 4:54pm Feb 12, 2010 at 4:54pm UTC
Feb 12, 2010 at 5:31pm Feb 12, 2010 at 5:31pm UTC
Change line 8 to
double d = c / 100.0 ;
100 is an integer. int / int = int. 100.0 is double. int / double = double.
Feb 13, 2010 at 1:35am Feb 13, 2010 at 1:35am UTC
why has nobody stated the obvious yet?
double x= 345.2359745;
printf ("%0.3d", x);
it doesn't change the actual variable, but only chops off the end parts on output.
Last edited on Feb 13, 2010 at 1:37am Feb 13, 2010 at 1:37am UTC
Feb 13, 2010 at 1:40am Feb 13, 2010 at 1:40am UTC
I think the op wanted to change the actual thing though. setprecision was brought up in the beginning of the discussion.