Android 文件大小转换kb,mb 等系统工具,不用自己写
Android系统方法:
package android.util;
DebugUtils.java
public static String sizeValueToString(long number, StringBuilder outBuilder) {
if (outBuilder == null) {
outBuilder = new StringBuilder(32);
}
float result = number;
String suffix = "";
if (result > 900) {
suffix = "KB";
result = result / 1024;
}
if (result > 900) {
suffix = "MB";
result = result / 1024;
}
if (result > 900) {
suffix = "GB";
result = result / 1024;
}
if (result > 900) {
suffix = "TB";
result = result / 1024;
}
if (result > 900) {
suffix = "PB";
result = result / 1024;
}
String value;
if (result < 1) {
value = String.format("%.2f", result);
} else if (result < 10) {
value = String.format("%.1f", result);
} else if (result < 100) {
value = String.format("%.0f", result);
} else {
value = String.format("%.0f", result);
}
outBuilder.append(value);
outBuilder.append(suffix);
return outBuilder.toString();
}