Advertisement
redandwhite

Readable file size from bytes in Java

Jul 31st, 2012
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.33 KB | None | 0 0
  1. public static String readableFileSize(long size) {
  2.     if(size <= 0) return "0";
  3.     final String[] units = new String[] { "B", "KB", "MB", "GB", "TB" };
  4.     int digitGroups = (int) (Math.log10(size)/Math.log10(1024));
  5.     return new DecimalFormat("#,##0.#").format(size/Math.pow(1024, digitGroups)) + " " + units[digitGroups];
  6. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement