Java tutorial
//package com.java2s; //License from project: Apache License public class Main { public static String getHumanReadableByteCount(final long bytes, final boolean si) { final int unit = si ? 1000 : 1024; if (bytes < unit) { return bytes + " B"; } else { final int exp = (int) (Math.log(bytes) / Math.log(unit)); final String pre = (si ? "kMGTPE" : "KMGTPE").charAt(exp - 1) + (si ? "" : "i"); return String.format("%.1f %sB", bytes / Math.pow(unit, exp), pre); } } }