Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save rayden-alex/b03d79d28078e36dea8bcac12dfea27c to your computer and use it in GitHub Desktop.
Save rayden-alex/b03d79d28078e36dea8bcac12dfea27c to your computer and use it in GitHub Desktop.
Format file size as readable string (kB, MB, GB)
// http://stackoverflow.com/a/5599842/225217
private static String asReadableFileSize(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