Here you can find the source of formatSize(long size)
public static String formatSize(long size)
//package com.java2s; //License from project: Apache License public class Main { public static String formatSize(long size) { if (size < 1024) return String.format("%d B", size); else if (size < 1024 * 1024) return String.format("%.2f KB", (double) size / 1024); else if (size < 1024 * 1024 * 1024) return String.format("%.2f MB", (double) size / (1024 * 1024)); else if (size < 1024L * 1024 * 1024 * 1024) return String.format("%.2f GB", (double) size / (1024 * 1024 * 1024)); else//from w w w .ja v a2 s . co m return String.format("%.2f EB", (double) size / (1024L * 1024 * 1024 * 1024)); } }