Skip to content

Commit

Permalink
Merge pull request #2418 from qq651901286/patch-1
Browse files Browse the repository at this point in the history
更新Update java-basic-questions-01.md文件对BigDecimal描述,比较值内容应该用compareTo
  • Loading branch information
Snailclimb authored Jul 1, 2024
2 parents f899fee + e03d26e commit c6ae419
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions docs/java/basis/java-basic-questions-01.md
Original file line number Diff line number Diff line change
Expand Up @@ -662,15 +662,18 @@ System.out.println(a == b);// false

```java
BigDecimal a = new BigDecimal("1.0");
BigDecimal b = new BigDecimal("0.9");
BigDecimal b = new BigDecimal("1.00");
BigDecimal c = new BigDecimal("0.8");

BigDecimal x = a.subtract(b);
BigDecimal x = a.subtract(c);
BigDecimal y = b.subtract(c);

System.out.println(x); /* 0.1 */
System.out.println(y); /* 0.1 */
System.out.println(Objects.equals(x, y)); /* true */
System.out.println(x); /* 0.2 */
System.out.println(y); /* 0.20 */
// 比较内容,不是比较值
System.out.println(Objects.equals(x, y)); /* false */
// 比较值相等用相等compareTo,相等返回0
System.out.println(0 == x.compareTo(y)); /* true */
```

关于 `BigDecimal` 的详细介绍,可以看看我写的这篇文章:[BigDecimal 详解](https://javaguide.cn/java/basis/bigdecimal.html)
Expand Down

0 comments on commit c6ae419

Please sign in to comment.