Skip to content

Commit

Permalink
NumberUtil.toBigDecimal转换科学计数法问题
Browse files Browse the repository at this point in the history
  • Loading branch information
looly committed Aug 2, 2023
1 parent a0eeefe commit c45b3fc
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 12 deletions.
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
15 changes: 5 additions & 10 deletions hutool-core/src/main/java/cn/hutool/core/util/NumberUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -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"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit c45b3fc

Please # to comment.