format Size with if else ladder - Android java.lang

Android examples for java.lang:long

Description

format Size with if else ladder

Demo Code

public class Main {

  public static String formatSize(long size) {
    long l = size;
    String s = "GB";
    if (l > 1024 * 1024 * 1024) {
      l /= 1024 * 1024 * 1024;// w  w  w  . j  av a2s .c  o m
    } else if (l > 1024 * 1024) {
      l /= 1024 * 1024;
      s = "MB";
    } else if (l > 1024) {
      l /= 1024;
      s = "KB";
    } else {
      s = "B";
    }

    return String.valueOf(l) + s;

  }

}

Related Tutorials