Here you can find the source of formatSize(long bytes)
public static String formatSize(long bytes)
//package com.java2s; //License from project: Open Source License import java.text.DecimalFormat; public class Main { public static String formatSize(long bytes) { if (bytes < 1024) return bytes + " B"; else if (bytes < 1024 * 1024) return singleDecimalDigit(bytes / 1024.0) + " KB"; else if (bytes < 1024 * 1024 * 1024) return singleDecimalDigit(bytes / 1024.0 / 1024.0) + " MB"; else/*from w w w . jav a2 s . co m*/ return singleDecimalDigit(bytes / 1024.0 / 1024.0 / 1024.0) + " GB"; } public static String singleDecimalDigit(double d) { return new DecimalFormat("#.#").format(d); } }