diff --git a/tongran-common/tongran-common-core/src/main/java/com/tongran/common/core/utils/UnitChangeUtil.java b/tongran-common/tongran-common-core/src/main/java/com/tongran/common/core/utils/UnitChangeUtil.java index 8317472..e360b1d 100644 --- a/tongran-common/tongran-common-core/src/main/java/com/tongran/common/core/utils/UnitChangeUtil.java +++ b/tongran-common/tongran-common-core/src/main/java/com/tongran/common/core/utils/UnitChangeUtil.java @@ -168,4 +168,24 @@ public class UnitChangeUtil { return String.format("%.2f %s", roundedValue, units[unitIndex]); } + public static String convertUnitByValue(Long value) { + if (value == null || value == 0) { + return "0 Kb"; + } + + // 注意:这里使用二进制单位(1024) + if (value >= 1024L * 1024 * 1024 * 1024) { + double result = value / (1024.0 * 1024 * 1024 * 1024); + return String.format("%.2f Tb", result); + } else if (value >= 1024L * 1024 * 1024) { // >= 1GB + double result = value / (1024.0 * 1024 * 1024); + return String.format("%.2f Gb", result); + } else if (value >= 1024L * 1024) { // >= 1MB + double result = value / (1024.0 * 1024); + return String.format("%.2f Mb", result); + } else { + double result = value / 1024.0; + return String.format("%.2f Kb", result); + } + } }