From b4a6fdc278535a01b95649a1fc9308833194fc2f Mon Sep 17 00:00:00 2001 From: liushuang Date: Mon, 29 Sep 2025 10:42:44 +0800 Subject: [PATCH] =?UTF-8?q?feat(core):=E4=BC=98=E5=8C=96=E6=95=B0=E5=80=BC?= =?UTF-8?q?=E8=BD=AC=E6=8D=A2=E9=80=BB=E8=BE=91=E4=BB=A5=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E7=A7=91=E5=AD=A6=E8=AE=A1=E6=95=B0=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 移除不必要的 Nullable 注解导入- 新增 isScientificNotation 方法用于检测科学计数法格式 - 修改 Double 类型数值转换逻辑,区分科学计数法与普通数值- 对于无法转换的科学计数法数值,记录警告日志并返回原始值 - 添加详细的 JavaDoc 注释说明科学计数法判断规则 --- .../cn/isliu/core/utils/GenerateUtil.java | 32 ++++++++++++++++--- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/src/main/java/cn/isliu/core/utils/GenerateUtil.java b/src/main/java/cn/isliu/core/utils/GenerateUtil.java index ebc0cb2..7d08b1d 100644 --- a/src/main/java/cn/isliu/core/utils/GenerateUtil.java +++ b/src/main/java/cn/isliu/core/utils/GenerateUtil.java @@ -4,7 +4,6 @@ import cn.isliu.core.FileData; import cn.isliu.core.annotation.TableProperty; import cn.isliu.core.enums.BaseEnum; import cn.isliu.core.enums.FileType; -import org.jetbrains.annotations.Nullable; import java.lang.reflect.Field; import java.lang.reflect.ParameterizedType; @@ -24,6 +23,7 @@ import java.util.stream.Collectors; public class GenerateUtil { // 使用统一的FsLogger替代java.util.logging.Logger + /** * 根据配置和数据生成DTO对象(通用版本) * @@ -277,14 +277,18 @@ public class GenerateUtil { result = bigDecimal.stripTrailingZeros().toPlainString(); } else if (value instanceof Double) { String stringValue = convertValue(value); - try { - result = String.valueOf(Long.parseLong(stringValue)); - } catch (NumberFormatException e) { + + // 判断是否为科学计数法 + if (isScientificNotation(stringValue)) { try { result = String.valueOf(((Double) Double.parseDouble(stringValue)).longValue()); } catch (NumberFormatException ex) { - throw new IllegalArgumentException("无法将值 '" + stringValue + "' 转换为 long 或科学计数法表示的数值", ex); + FsLogger.warn("无法将科学计数法值 '" + stringValue + "' 转换为 long, 直接返回原始值"); + result = stringValue; } + } else { + // 不是科学计数法,直接返回原始字符串值 + result = stringValue; } } else { result = String.valueOf(value); @@ -303,6 +307,24 @@ public class GenerateUtil { return result.trim(); } + /** + * 判断字符串是否为科学计数法格式 + * 科学计数法格式:数字 + E/e + 数字,如 1.23E+4, 1.23e-2 + * + * @param value 要判断的字符串 + * @return 如果是科学计数法返回true,否则返回false + */ + private static boolean isScientificNotation(String value) { + if (value == null || value.trim().isEmpty()) { + return false; + } + + String trimmed = value.trim(); + // 使用正则表达式匹配科学计数法格式 + // 匹配格式:数字(可选小数点) + E/e + 可选正负号 + 数字 + return trimmed.matches("^[+-]?\\d+(\\.\\d+)?[eE][+-]?\\d+$"); + } + private static Object convertValue(Object value, Class targetType) throws Exception { if (value == null) return null;