File tree Expand file tree Collapse file tree 1 file changed +8
-5
lines changed
Expand file tree Collapse file tree 1 file changed +8
-5
lines changed Original file line number Diff line number Diff line change @@ -662,15 +662,18 @@ System.out.println(a == b);// false
662662
663663``` java
664664BigDecimal a = new BigDecimal (" 1.0" );
665- BigDecimal b = new BigDecimal (" 0.9 " );
665+ BigDecimal b = new BigDecimal (" 1.00 " );
666666BigDecimal c = new BigDecimal (" 0.8" );
667667
668- BigDecimal x = a. subtract(b );
668+ BigDecimal x = a. subtract(c );
669669BigDecimal y = b. subtract(c);
670670
671- System . out. println(x); /* 0.1 */
672- System . out. println(y); /* 0.1 */
673- System . out. println(Objects . equals(x, y)); /* true */
671+ System . out. println(x); /* 0.2 */
672+ System . out. println(y); /* 0.20 */
673+ // 比较内容,不是比较值
674+ System . out. println(Objects . equals(x, y)); /* false */
675+ // 比较值相等用相等compareTo,相等返回0
676+ System . out. println(0 == x. compareTo(y)); /* true */
674677```
675678
676679关于 ` BigDecimal ` 的详细介绍,可以看看我写的这篇文章:[ BigDecimal 详解] ( https://javaguide.cn/java/basis/bigdecimal.html ) 。
You can’t perform that action at this time.
0 commit comments