由byte格式化为KB、MB、GB
注意传参为基本类型 long,而不能为包装类 Long
private static String byteFormat(long size) {
StringBuffer bytes = new StringBuffer();
DecimalFormat format = new DecimalFormat("### ###.0");
if (size >= 1024 * 1024 * 1024) {
double i = (size / (1024.0 * 1024.0 * 1024.0));
bytes.append(format.format(i)).append("GB");
}
else if (size >= 1024 * 1024) {
double i = (size / (1024.0 * 1024.0));
bytes.append(format.format(i)).append("MB");
}
else if (size >= 1024) {
double i = (size / (1024.0));
bytes.append(format.format(i)).append("KB");
}
else if (size < 1024) {
if (size <= 0) {
bytes.append("0B");
}
else {
bytes.append((int) size).append("B");
}
}
return bytes.toString();
}