double a = 99.99;
double b = 1.05;
double dsum = a + b;
double dsub = a-b;
System.out.println(dsum); //101.03999999999999
System.out.println(dsub); //98.94
float c = 99.99f;
float d = 1.05f;
float fsum = c+d;
float fsub = c-d;
System.out.println(fsum); //101.04
System.out.println(fsub); //98.939995
Check these additions and subtractions. Can someone explain why is there results like these with numbers ending with 9. My application is dealing with money so error of even .001 is not acceptable. In IOS swift also there is this issue but when using Decimal it works fine.
One theory is that decimal and and float save 1 as 0.99999, thus 1.05 becomes 1.04999(hence giving 101.03999) but then why is this not consistent with all values?