Here you can find the source of formatFileSize(long fileLength)
public static String formatFileSize(long fileLength)
//package com.java2s; import java.text.DecimalFormat; public class Main { public static String formatFileSize(long fileLength) { DecimalFormat df = new DecimalFormat("#.00"); String fileSizeStr = ""; if (fileLength < 1024) fileSizeStr = df.format((double) fileLength) + "B"; else if (fileLength < 1048576) fileSizeStr = df.format((double) fileLength / 1024) + "KB"; else if (fileLength < 1073741824) fileSizeStr = df.format((double) fileLength / 1048576) + "MB"; else//from w w w . j ava2 s .c om fileSizeStr = df.format((double) fileLength / 1073741824) + "GB"; return fileSizeStr; } }