I am trying to extract only the decimal digits in a number (e.g. 4.375 becomes 0.375). For this purpose I wrote this snippet of code, that works mostly as expected, except for the last float.
float[] actualSizes = {4.375f, 4.375f, 6.5625f, 8.75f, 10.93f};
float decimalDigits;
for (int i = 0; i < actualSizes.length; i++)
{
decimalDigits = actualSizes[i] - (int)(actualSizes[i]);
System.out.println(decimalDigits);
}
Here is the output, I expected the last line to be 0.93:
0.375
0.375
0.5625
0.75
0.9300003
I have tried the same using double, and the result is again not the one expected. I think the problem might be along the lines of the one described in the question Is floating point math broken? (The question is for Javascript, while I'm using Java).
Is there a way to get around this in my program and get that 0.93?