Java tutorial
//package com.java2s; //License from project: Open Source License public class Main { public static String humanReadableCount(long bytes, boolean si) { int unit = si ? 1000 : 1024; if (bytes < unit) return bytes + " B"; int exp = (int) (Math.log(bytes) / Math.log(unit)); char pre = "KMGTPE".charAt(exp - 1); return String.format("%.2f %s" + (si ? "bps" : "B"), bytes / Math.pow(unit, exp), pre); } }