From e03d26e20324052b167411aafddfdaccad906716 Mon Sep 17 00:00:00 2001 From: qixiao <60742716+qq651901286@users.noreply.github.com> Date: Sun, 30 Jun 2024 22:23:35 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0Update=20java-basic-questions?= =?UTF-8?q?-01.md=E6=96=87=E4=BB=B6=E5=AF=B9BigDecimal=E6=8F=8F=E8=BF=B0?= =?UTF-8?q?=EF=BC=8C=E6=AF=94=E8=BE=83=E5=80=BC=E5=86=85=E5=AE=B9=E5=BA=94?= =?UTF-8?q?=E8=AF=A5=E7=94=A8compareTo?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/java/basis/java-basic-questions-01.md | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/docs/java/basis/java-basic-questions-01.md b/docs/java/basis/java-basic-questions-01.md index 3e510212e76..de037a3aa3d 100644 --- a/docs/java/basis/java-basic-questions-01.md +++ b/docs/java/basis/java-basic-questions-01.md @@ -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)。