The double type and float type are primarily designed for use in scientific and engineering calculations and are used for binary floating-point calculations. For the general calculation is usually inaccurate results, so for the ordinary floating-point addition and subtraction, the solution needs to use Int,long,bigdecimal to calculate.
1. Calculate the problem?
The calculation results are as follows:
Obviously the result is not what we need. The result we need is: 0.88
So, our program should write this:
1 Public Static void Main (string[] args) {2 BigDecimal a=bigdecimal.valueof (1.29); 3 BigDecimal b=bigdecimal.valueof (0.41); 4 System.out.println (a.subtract (b)); 5 }
The final result is also the result we need:
This is a long time to see ...
In Java, if you need an accurate calculation answer, avoid using double type and float type