Files
c-api/src/main/java/com/mini/capi/utils/vF.java

29 lines
615 B
Java
Raw Normal View History

2025-08-24 21:42:47 +08:00
package com.mini.capi.utils;
public class vF {
private static final String[] UNITS = {"B", "K", "M", "G", "T", "P"};
public static String format(long bytes) {
if (bytes < 0) {
throw new IllegalArgumentException("字节数不能为负数");
}
int unitIndex = 0;
double size = bytes;
while (size >= 1024 && unitIndex < UNITS.length - 1) {
size /= 1024;
unitIndex++;
}
// 保留最多一位小数,去掉多余的 .0
return String.format("%.1f", size).replace(".0", "") + UNITS[unitIndex];
}
}