Human readable numbers

From Null-pointer

Jump to: navigation, search

human readable numbers [1]

public static String humanReadableByteCount(long bytes, boolean si) {
    int unit = si ? 1000 : 1024;
    if (bytes < unit) return bytes + " B";
    int exp = (int) (Math.log(bytes) / Math.log(unit));
    String pre = (si ? "kMGTPE" : "KMGTPE").charAt(exp-1) + (si ? "" : "i");
    return String.format("%.1f %sB", bytes / Math.pow(unit, exp), pre);
}


References

  1. aioobe. (10 Sept 2010). "How to convert byte size into human readable format in java?" Retrieved 10 Mar 2011.
Personal tools