While I don't know if apache commons has anything like this, but you can easily write a static helper class that has this function inside of it:
public static String fileSize(double bytes, boolean si) {
int thresh = si ? 1000 : 1024;
if (Math.abs(bytes) < thresh) {
return bytes + "B";
}
String[] units = (si ? new String[] { "kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB" }
: new String[] { "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB" });
int u = -1;
do {
bytes /= thresh;
++u;
} while (Math.abs(bytes) >= thresh && u < units.length - 1);
return String.format("%.1f", bytes) + "" + units[u];
}
This will continually divide the input double bytes until it becomes lower than the threshold, and each division will increase the index of the String array that holds the units.