From 29a30b1f5228ad407f25897cfa02aaf00bb727b5 Mon Sep 17 00:00:00 2001 From: gaoyutao Date: Wed, 28 Jan 2026 09:46:54 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A1=A5=E5=85=85=E5=8D=95=E4=BD=8D=E8=BD=AC?= =?UTF-8?q?=E5=8C=96=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/core/utils/UnitChangeUtil.java | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) 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); + } + } }