补充单位转化方法
This commit is contained in:
+20
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user