I want to subtract 2 double values, and I tried the following code.
double val1 = 2.0; double val2 = 1.10; System.out.println(val1 - val2);
and I got the result as
0.8999999999999999
To get output 0.9 I tried with BigDecimal as follows:
BigDecimal val1BD = new BigDecimal(val1); BigDecimal val2BD = new BigDecimal(val2); System.out.println(val1BD.subtract(val2BD));
And I got a conclusion like,
0.899999999999999911182158029987476766109466552734375
Then I tried using BigDecimal.valueOf()
val1BD = BigDecimal.valueOf(val1); val2BD = BigDecimal.valueOf(val2); System.out.println(val1BD.subtract(val2BD));
And finally, I got the output as 0.9 .
My question is what is the difference between case 2 and case 3?
In case 2, why did I get such a conclusion?
source share