0

Say I have the number 3.3, and I want to get just the decimal part. What I did was (I'm separating the code out to do one thing per line as much as possible):

float _x = 3.3;
float _j = _x - floor(_x);
printf("1. %f\n", _j);
_j *= 10;
printf("2. %f\n", _j);
int z = (int) _j;
printf("3. %d\n", z);

I'm getting the following output:

1. 0.300000
2. 3.000000
3. 2

What's happening in the casting of _j to int that causes the number to equal 2 instead?

galois
  • 825
  • 2
  • 11
  • 31

0 Answers0