Format long value to readable File Size using DecimalFormat - Android java.text

Android examples for java.text:DecimalFormat

Description

Format long value to readable File Size using DecimalFormat

Demo Code

import java.text.DecimalFormat;

public class Main {

  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];
  }/*from ww  w  .ja v a2s  . co  m*/

}

Related Tutorials