Here you can find the source of formatSize(long size)
public static String formatSize(long size)
//package com.java2s; //License from project: Open Source License public class Main { public static final long ONE_KILOBYTE = 1000; public static final long ONE_MEGABYTE = 1000 * ONE_KILOBYTE; public static final long ONE_GIGABYTE = 1000 * ONE_MEGABYTE; public static String formatSize(long size) { if (size >= 100 * ONE_GIGABYTE) return String.format("%,d GB", size / ONE_GIGABYTE); if (size >= 10 * ONE_GIGABYTE) return String.format("%.1f GB", (double) size / ONE_GIGABYTE); if (size >= ONE_GIGABYTE) return String.format("%.2f GB", (double) size / ONE_GIGABYTE); if (size >= 10 * ONE_MEGABYTE) return String.format("%,d MB", size / ONE_MEGABYTE); if (size >= ONE_MEGABYTE) return String.format("%.1f MB", (double) size / ONE_MEGABYTE); if (size >= ONE_KILOBYTE) return String.format("%,d KB", size / ONE_KILOBYTE); return String.format("%,d bytes", size); }//w w w . ja v a 2 s. c om }