mirror of
https://gitee.com/dromara/hutool.git
synced 2026-02-09 09:16:26 +08:00
fix: correct NumberUtil.parseNumber scientific notation parsing, add parseNumberTest5
This commit is contained in:
@@ -2783,6 +2783,16 @@ public class NumberUtil {
|
||||
// issue@4197@Github 转为半角
|
||||
numberStr = Convert.toDBC(numberStr);
|
||||
|
||||
// issue#IDJ1NS@Gitee 处理科学计数法E+格式
|
||||
// NumberFormat对E+格式支持不佳,使用BigDecimal直接解析
|
||||
if (numberStr.contains("E") || numberStr.contains("e")) {
|
||||
try {
|
||||
return new BigDecimal(numberStr);
|
||||
} catch (NumberFormatException e) {
|
||||
// BigDecimal解析失败,继续使用NumberFormat尝试
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
final NumberFormat format = NumberFormat.getInstance();
|
||||
if (format instanceof DecimalFormat) {
|
||||
|
||||
@@ -388,6 +388,17 @@ 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());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parseHexNumberTest() {
|
||||
// 千位分隔符去掉
|
||||
|
||||
Reference in New Issue
Block a user