If you think the Android project filemanager listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
Java Source Code
package com.binkery.app.filemanager.utils;
/*fromwww.java2s.com*/import java.util.Locale;
publicclass StringUtils {
publicstatic String formatFileSize(long number) {
return formatFileSize(number, false);
}
publicstatic String formatShortFileSize(long number) {
return formatFileSize(number, true);
}
privatestatic String formatFileSize(long number, boolean shorter) {
float result = number;
String suffix = "byte";
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 == 0){
value = "0";
}elseif (result < 1) {
value = String.format(Locale.US, "%.2f", result);
} elseif (result < 10) {
if (shorter) {
value = String.format(Locale.US, "%.1f", result);
} else {
value = String.format(Locale.US, "%.2f", result);
}
} elseif (result < 100) {
if (shorter) {
value = String.format(Locale.US, "%.0f", result);
} else {
value = String.format(Locale.US, "%.2f", result);
}
} else {
value = String.format(Locale.US, "%.0f", result);
}
return value + suffix;
}
}