From c45b3fccdab453bb1c6007b52b18bd2fbdb68e99 Mon Sep 17 00:00:00 2001 From: Looly Date: Wed, 2 Aug 2023 17:07:31 +0800 Subject: [PATCH] =?UTF-8?q?NumberUtil.toBigDecimal=E8=BD=AC=E6=8D=A2?= =?UTF-8?q?=E7=A7=91=E5=AD=A6=E8=AE=A1=E6=95=B0=E6=B3=95=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 5 ++-- .../java/cn/hutool/core/util/NumberUtil.java | 15 ++++------- .../cn/hutool/core/convert/Issue3241Test.java | 25 +++++++++++++++++++ .../cn/hutool/core/util/NumberUtilTest.java | 2 ++ 4 files changed, 35 insertions(+), 12 deletions(-) create mode 100755 hutool-core/src/test/java/cn/hutool/core/convert/Issue3241Test.java diff --git a/CHANGELOG.md b/CHANGELOG.md index 8c3f73fe23..d97b6b2a40 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,10 +5,11 @@ # 5.8.22(2023-08-02) ### 🐣新特性 -* 【core 】 NumberUtil.nullToZero增加重载(issue#I7PPD2@Github) -* 【core 】 DesensitizedUtil增加清空策略(issue#I7PUJ2@Github) +* 【core 】 NumberUtil.nullToZero增加重载(issue#I7PPD2@Gitee) +* 【core 】 DesensitizedUtil增加清空策略(issue#I7PUJ2@Gitee) ### 🐞Bug修复 +* 【core 】 NumberUtil.toBigDecimal转换科学计数法问题(issue#3241@Github) ------------------------------------------------------------------------------------------------------------- # 5.8.21(2023-07-29) diff --git a/hutool-core/src/main/java/cn/hutool/core/util/NumberUtil.java b/hutool-core/src/main/java/cn/hutool/core/util/NumberUtil.java index 59bf9427dd..f79ac9e000 100755 --- a/hutool-core/src/main/java/cn/hutool/core/util/NumberUtil.java +++ b/hutool-core/src/main/java/cn/hutool/core/util/NumberUtil.java @@ -2240,19 +2240,14 @@ public static BigDecimal toBigDecimal(String numberStr) { return BigDecimal.ZERO; } - try { - // 支持类似于 1,234.55 格式的数字 - final Number number = parseNumber(numberStr); - if (number instanceof BigDecimal) { - return (BigDecimal) number; - } else { - return new BigDecimal(number.toString()); - } - } catch (Exception ignore) { + try{ + return new BigDecimal(numberStr); + } catch (Exception ignore){ // 忽略解析错误 } - return new BigDecimal(numberStr); + // 支持类似于 1,234.55 格式的数字 + return toBigDecimal(parseNumber(numberStr)); } /** diff --git a/hutool-core/src/test/java/cn/hutool/core/convert/Issue3241Test.java b/hutool-core/src/test/java/cn/hutool/core/convert/Issue3241Test.java new file mode 100755 index 0000000000..66610be464 --- /dev/null +++ b/hutool-core/src/test/java/cn/hutool/core/convert/Issue3241Test.java @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2023 looly(loolly@aliyun.com) + * Hutool is licensed under Mulan PSL v2. + * You can use this software according to the terms and conditions of the Mulan PSL v2. + * You may obtain a copy of Mulan PSL v2 at: + * http://license.coscl.org.cn/MulanPSL2 + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, + * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, + * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. + * See the Mulan PSL v2 for more details. + */ + +package cn.hutool.core.convert; + +import org.junit.Assert; +import org.junit.Test; + +import java.math.BigDecimal; + +public class Issue3241Test { + @Test + public void toBigDecimalTest() { + Assert.assertEquals(new BigDecimal("9.0E+7"), Convert.toBigDecimal("9.0E+7")); + } +} diff --git a/hutool-core/src/test/java/cn/hutool/core/util/NumberUtilTest.java b/hutool-core/src/test/java/cn/hutool/core/util/NumberUtilTest.java index 9edca4ddf5..822b8630b7 100644 --- a/hutool-core/src/test/java/cn/hutool/core/util/NumberUtilTest.java +++ b/hutool-core/src/test/java/cn/hutool/core/util/NumberUtilTest.java @@ -249,6 +249,8 @@ public void toBigDecimalTest() { bigDecimal = NumberUtil.toBigDecimal("1,234.56D"); Assert.assertEquals("1234.56", bigDecimal.toString()); + + Assert.assertEquals(new BigDecimal("9.0E+7"), NumberUtil.toBigDecimal("9.0E+7")); } @Test