Here you can find the source of formatFileSize(long length)
public static String formatFileSize(long length)
//package com.java2s; public class Main { public static String formatFileSize(long length) { String result = null;//from w w w.j av a 2 s . co m int sub_string = 0; if (length >= 1073741824) { sub_string = String.valueOf((float) length / 1073741824) .indexOf("."); result = ((float) length / 1073741824 + "000").substring(0, sub_string + (length % 1073741824 > 0 ? 3 : 0)) + "G"; } else if (length >= 1048576) { sub_string = String.valueOf((float) length / 1048576).indexOf( "."); result = ((float) length / 1048576 + "000").substring(0, sub_string + (length % 1048576 > 0 ? 3 : 0)) + "M"; } else if (length >= 1024) { sub_string = String.valueOf((float) length / 1024).indexOf("."); result = ((float) length / 1024 + "000").substring(0, sub_string + (length % 1024 > 0 ? 3 : 0)) + "KB"; } else if (length < 1024) result = Long.toString(length) + "B"; return result; } }