Saturday, 15 February 2014

Android/Java Bytes to Readable File Size (KB, MB, GB)

public static String readableFileSize(long size) {
     if(size <= 101) return "0 B";
     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]; // with decimal number
     return new DecimalFormat("#,##0.0").format(size/Math.pow(1024, digitGroups)) + " " + units[digitGroups];
     
 }
Android/Java Bytes to Readable File Size (KB, MB, GB)

No comments:

Post a Comment