Skip to content

Instantly share code, notes, and snippets.

@lalitsharma1607
Created April 26, 2017 10:04
Show Gist options
  • Save lalitsharma1607/120f2fc881f8331eef96e7c0e04b5c2c to your computer and use it in GitHub Desktop.
Save lalitsharma1607/120f2fc881f8331eef96e7c0e04b5c2c to your computer and use it in GitHub Desktop.
Human readable file size format with units | Format file size as MB, GB etc
public class FileUtils{
public static String readableFileSize(long size) {
if(size <= 0) return "0";
final String[] units = new String[] { "B", "kB", "MB", "GB", "TB" };
int digitGroups = (int) (Math.log10(size)/Math.log10(1024));
return new DecimalFormat("#,##0.#").format(size/Math.pow(1024, digitGroups)) + " " + units[digitGroups];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment