Here you can find the source of byteToHuman(long bytes, boolean si)
public static String byteToHuman(long bytes, boolean si)
//package com.java2s; //License from project: Apache License public class Main { public static String byteToHuman(long bytes, boolean si) { int unit = si ? 1000 : 1024; if (bytes < unit) { return bytes + " B"; }//from www. ja v a2s.co m 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); } public static String byteToHuman(long bytes) { int unit = 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("%.1f%s", bytes / Math.pow(unit, exp), pre); } }