Skip to content

Commit c45b3fc

Browse files
committed
NumberUtil.toBigDecimal转换科学计数法问题
1 parent a0eeefe commit c45b3fc

File tree

4 files changed

+35
-12
lines changed

4 files changed

+35
-12
lines changed

CHANGELOG.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@
55
# 5.8.22(2023-08-02)
66

77
### 🐣新特性
8-
* 【core 】 NumberUtil.nullToZero增加重载(issue#I7PPD2@Github
9-
* 【core 】 DesensitizedUtil增加清空策略(issue#I7PUJ2@Github
8+
* 【core 】 NumberUtil.nullToZero增加重载(issue#I7PPD2@Gitee
9+
* 【core 】 DesensitizedUtil增加清空策略(issue#I7PUJ2@Gitee
1010

1111
### 🐞Bug修复
12+
* 【core 】 NumberUtil.toBigDecimal转换科学计数法问题(issue#3241@Github)
1213

1314
-------------------------------------------------------------------------------------------------------------
1415
# 5.8.21(2023-07-29)

hutool-core/src/main/java/cn/hutool/core/util/NumberUtil.java

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2240,19 +2240,14 @@ public static BigDecimal toBigDecimal(String numberStr) {
22402240
return BigDecimal.ZERO;
22412241
}
22422242

2243-
try {
2244-
// 支持类似于 1,234.55 格式的数字
2245-
final Number number = parseNumber(numberStr);
2246-
if (number instanceof BigDecimal) {
2247-
return (BigDecimal) number;
2248-
} else {
2249-
return new BigDecimal(number.toString());
2250-
}
2251-
} catch (Exception ignore) {
2243+
try{
2244+
return new BigDecimal(numberStr);
2245+
} catch (Exception ignore){
22522246
// 忽略解析错误
22532247
}
22542248

2255-
return new BigDecimal(numberStr);
2249+
// 支持类似于 1,234.55 格式的数字
2250+
return toBigDecimal(parseNumber(numberStr));
22562251
}
22572252

22582253
/**
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Copyright (c) 2023 looly([email protected])
3+
* Hutool is licensed under Mulan PSL v2.
4+
* You can use this software according to the terms and conditions of the Mulan PSL v2.
5+
* You may obtain a copy of Mulan PSL v2 at:
6+
* http://license.coscl.org.cn/MulanPSL2
7+
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
8+
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
9+
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
10+
* See the Mulan PSL v2 for more details.
11+
*/
12+
13+
package cn.hutool.core.convert;
14+
15+
import org.junit.Assert;
16+
import org.junit.Test;
17+
18+
import java.math.BigDecimal;
19+
20+
public class Issue3241Test {
21+
@Test
22+
public void toBigDecimalTest() {
23+
Assert.assertEquals(new BigDecimal("9.0E+7"), Convert.toBigDecimal("9.0E+7"));
24+
}
25+
}

hutool-core/src/test/java/cn/hutool/core/util/NumberUtilTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,8 @@ public void toBigDecimalTest() {
249249

250250
bigDecimal = NumberUtil.toBigDecimal("1,234.56D");
251251
Assert.assertEquals("1234.56", bigDecimal.toString());
252+
253+
Assert.assertEquals(new BigDecimal("9.0E+7"), NumberUtil.toBigDecimal("9.0E+7"));
252254
}
253255

254256
@Test

0 commit comments

Comments
 (0)