I was writing a simple program that happens to multiply a double by 10 a few times dependent on a loop count variable and with everything i put in to it. It returned the correct value but for some reason when the double 0.802 was multiplied by 10 three times this is what happened. 0.802 * 10 = 8.02. 8.02 * 10 = 80.1999999999. 80.1999999999 * 10 = 801.99999999999. (dont mind how many nines are on the end of each of those).
Essentially i thought this had to do with something regarding the behavior of a double and the conversion from int to double so i tried casting entire expression and changing 10 to 10d but this dident work. I have a suspicion that i have to use Math.round but this program i was hoping to be independent from any outside method calls so alternative fixes would be appreciated.
This is some test code i used the first one spits out the correct answer while the second does not.
1.
double number = 0.802;
number = (double) number * 1000;
2.
double number = 0.802;
number = (double) number * 10;
number = (double) number * 10;
number = (double) number * 10;
This code when run should give the answer 802.