When I run the following code:
double x = 4.35;
double y = x * 100;
System.out.println(y);
I get 434.99999999999994 which is expected due to the nature of floating point precision. However when I run:
double x = 4.35;
double y = x * 1000;
System.out.println(y);
I get 4350.0 in the console. Similarly when I replace the 1000 with 10, I get 43.5, which is peculiar, since there is no rounding error for these numbers. It seems that there is only rounding error when I mulptiply the double by 10 to the power of the number of decimal places. Why is it like that?
Also, when I tried running the three blocks of code using 4.25 as my value for x, I got a nice 425.0 in the console. Does rounding error only apply to some decimal values?
EDIT: Dividing 4.35 by 10 also gave me a wacky decimal (0.43499999999999994 to be exact). Also, if I set x to 43.5, there were no rounding errors at all. Multiplying by 10 gave me 435.0.
EDIT 2: I think some people are misinterpreting my question. I'm asking why this happens and not how to avoid running into these errors.
 
     
    