Here you can find the source of formatSize(long size)
public static String formatSize(long size)
//package com.java2s; //License from project: LGPL public class Main { public static String formatSize(long size) { long SIZE_KB = 1024; long SIZE_MB = SIZE_KB * 1024; long SIZE_GB = SIZE_MB * 1024; if (size < SIZE_KB) { return String.format("%d B", (int) size); } else if (size < SIZE_MB) { return String.format("%.2f KB", (float) size / SIZE_KB); } else if (size < SIZE_GB) { return String.format("%.2f MB", (float) size / SIZE_MB); } else {/*from w ww . j a v a 2s .c o m*/ return String.format("%.2f GB", (float) size / SIZE_GB); } } }