fix:内存大小单位转换修复 (#602)

* fix:内存大小单位转换修复

* ByteConvertUtil补充测试用例
pull/605/head
Alic 2 years ago committed by GitHub
parent f5285e4e00
commit 4874cdad4d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -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";
}
}
}

@ -19,10 +19,15 @@ package cn.hippo4j.common.toolkit;
import org.junit.Test;
import java.util.Objects;
public class ByteConvertUtilTest {
@Test
public void assertGetPrintSize() {
Assert.isTrue(Objects.equals(ByteConvertUtil.getPrintSize(220), "220B"));
Assert.isTrue(Objects.equals(ByteConvertUtil.getPrintSize(2200), "2.15KB"));
Assert.isTrue(Objects.equals(ByteConvertUtil.getPrintSize(2200000), "2.10MB"));
Assert.isTrue(Objects.equals(ByteConvertUtil.getPrintSize(2200000000L), "2.05GB"));
}
}

Loading…
Cancel
Save