NumberUtil.toBigDecimal转换科学计数法问题

This commit is contained in:
Looly
2023-08-02 17:07:31 +08:00
parent a0eeefefe9
commit c45b3fccda
4 changed files with 35 additions and 12 deletions

View File

@@ -2240,19 +2240,14 @@ public class NumberUtil {
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));
}
/**

View File

@@ -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"));
}
}

View File

@@ -249,6 +249,8 @@ public class NumberUtilTest {
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