mirror of
https://gitee.com/dromara/hutool.git
synced 2026-02-09 09:16:26 +08:00
NumberUtil.parseNumber增加支持科学计数法(pr#4211@Github)
This commit is contained in:
@@ -1,8 +1,9 @@
|
||||
|
||||
# 🚀Changelog
|
||||
-------------------------------------------------------------------------------------------------------------
|
||||
# 5.8.44
|
||||
# 5.8.44(2026-01-12)
|
||||
### 🐣新特性
|
||||
* 【core 】 `NumberUtil.parseNumber`增加支持科学计数法(pr#4211@Github)
|
||||
|
||||
### 🐞Bug修复
|
||||
* 【json 】 修复`JSONUtil.wrap`忽略错误问题(issue#4210@Github)
|
||||
|
||||
@@ -2785,7 +2785,7 @@ public class NumberUtil {
|
||||
|
||||
// issue#IDJ1NS@Gitee 处理科学计数法E+格式
|
||||
// NumberFormat对E+格式支持不佳,使用BigDecimal直接解析
|
||||
if (numberStr.contains("E") || numberStr.contains("e")) {
|
||||
if (StrUtil.containsIgnoreCase(numberStr, "e")) {
|
||||
try {
|
||||
return new BigDecimal(numberStr);
|
||||
} catch (NumberFormatException e) {
|
||||
|
||||
@@ -389,14 +389,13 @@ public class NumberUtilTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parseNumberTest5() {
|
||||
String numberstr1 = "8.37095942E+9";
|
||||
Number result1 = NumberUtil.parseNumber(numberstr1);
|
||||
// 转换成BigDecimal再输出完整数字
|
||||
System.out.println(((BigDecimal) result1).toPlainString());
|
||||
String numberstr2 = "8.37095942e+9";
|
||||
Number result2 = NumberUtil.parseNumber(numberstr2);
|
||||
System.out.println(((BigDecimal) result2).toPlainString());
|
||||
void issueIDJ1NSTest(){
|
||||
final String numberstr1 = "8.37095942E+9";
|
||||
final BigDecimal result1 = (BigDecimal) NumberUtil.parseNumber(numberstr1);
|
||||
final String numberstr2 = "8.37095942e+9";
|
||||
final BigDecimal result2 = (BigDecimal) NumberUtil.parseNumber(numberstr2);
|
||||
assertEquals(new BigDecimal("8370959420").toPlainString(), result1.toPlainString());
|
||||
assertEquals(new BigDecimal("8370959420").toPlainString(), result2.toPlainString());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Reference in New Issue
Block a user