原帖由 stanfish 於 2006-10-10 12:05 AM 發表
I tried System.out.println("1.1+1.01-2.11="+(1.1+1.01-2.11)); recently. Guess what it comes out! The answer is not zero, but a number very close to zero. I guess it is computation gap ...
I think that's the nature of float or double data type calculation. You will have to do some roundings to get the result right. If you really care about the precision, you might want to use BigDecimal.
The reasons why the calculation of float/double values does not yield to accurate result involves how float/double are stored in memory and more. I think you can probably find some explanation on the web if you are interested.