From 49a23787ebc0db337925d34f2c50f862a8854504 Mon Sep 17 00:00:00 2001 From: alic Date: Sun, 28 Aug 2022 14:10:37 +0800 Subject: [PATCH] =?UTF-8?q?fix:=E5=86=85=E5=AD=98=E5=A4=A7=E5=B0=8F?= =?UTF-8?q?=E5=8D=95=E4=BD=8D=E8=BD=AC=E6=8D=A2=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/toolkit/ByteConvertUtil.java | 28 +++++++++---------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/hippo4j-common/src/main/java/cn/hippo4j/common/toolkit/ByteConvertUtil.java b/hippo4j-common/src/main/java/cn/hippo4j/common/toolkit/ByteConvertUtil.java index b181692f..102ba478 100644 --- a/hippo4j-common/src/main/java/cn/hippo4j/common/toolkit/ByteConvertUtil.java +++ b/hippo4j-common/src/main/java/cn/hippo4j/common/toolkit/ByteConvertUtil.java @@ -17,29 +17,27 @@ package cn.hippo4j.common.toolkit; +import java.text.DecimalFormat; + /** * Byte conversion tool class */ public class ByteConvertUtil { + public static final Integer KB_SIZE = 2 << 9; + public static final Integer MB_SIZE = 2 << 19; + public static final Integer GB_SIZE = 2 << 29; + public static String getPrintSize(long size) { - long covertNum = 1024; - if (size < covertNum) { + DecimalFormat df = new DecimalFormat("#.00"); + if (size < KB_SIZE) { return size + "B"; + } else if (size < MB_SIZE) { + return df.format((double) size / KB_SIZE) + "KB"; + } else if (size < GB_SIZE) { + return df.format((double) size / MB_SIZE) + "MB"; } else { - size = size / covertNum; - } - if (size < covertNum) { - return size + "KB"; - } else { - size = size / covertNum; - } - if (size < covertNum) { - size = size * 100; - return (size / 100) + "." + (size % 100) + "MB"; - } else { - size = size * 100 / covertNum; - return (size / 100) + "." + (size % 100) + "GB"; + return df.format((double) size / GB_SIZE) + "GB"; } } }