Here you can find the source of formatSize(long size)
public static String formatSize(long size)
//package com.java2s; import java.text.DecimalFormat; public class Main { private static final String[] units = { " B", " KB", " MB", " GB", " TB", " PB" }; public static String formatSize(long size) { DecimalFormat df = new DecimalFormat("###,###.#"); long div = 1; long testval = size; for (int i = 0; i < 6; i++) { if ((testval >>>= 10) == 0) { return df.format((double) size / div) + units[i]; }/* w ww . j a v a2 s . co m*/ div <<= 10; } return size + units[0]; } }