Android Long Format formatFileSize(long length)

Here you can find the source of formatFileSize(long length)

Description

format File Size

License

Open Source License

Declaration

public static String formatFileSize(long length) 

Method Source Code

//package com.java2s;

public class Main {

    public static String formatFileSize(long length) {

        String result = null;//from   w w  w.j  av a 2  s  . co m

        int sub_string = 0;
        if (length >= 1073741824) {
            sub_string = String.valueOf((float) length / 1073741824)
                    .indexOf(".");
            result = ((float) length / 1073741824 + "000").substring(0,
                    sub_string + (length % 1073741824 > 0 ? 3 : 0)) + "G";
        } else if (length >= 1048576) {
            sub_string = String.valueOf((float) length / 1048576).indexOf(
                    ".");
            result = ((float) length / 1048576 + "000").substring(0,
                    sub_string + (length % 1048576 > 0 ? 3 : 0)) + "M";
        } else if (length >= 1024) {
            sub_string = String.valueOf((float) length / 1024).indexOf(".");
            result = ((float) length / 1024 + "000").substring(0,
                    sub_string + (length % 1024 > 0 ? 3 : 0)) + "KB";
        } else if (length < 1024)
            result = Long.toString(length) + "B";

        return result;
    }
}

Related

  1. formatSize(long value)
  2. formatSpaceSize(long file_size)
  3. formatPercent(long divisor, long dividend)
  4. formatSize(long value)
  5. getTimeFormattedFromMillis(long millis)
  6. ReadableByteCount(long bytes)
  7. getDisplayable(long numBytes)
  8. formatByte(long l)
  9. formatFromSize(long size)