Android Long Readable Format byteCountToDisplaySize(long size)

Here you can find the source of byteCountToDisplaySize(long size)

Description

byte Count To Display Size

License

Apache License

Declaration

public static String byteCountToDisplaySize(long size) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {
    /**/*from   w  w  w .j  ava  2s.co  m*/
     * The number of bytes in a kilobyte.
     */
    public static final long ONE_KB = 1024;
    /**
     * The number of bytes in a megabyte.
     */
    public static final long ONE_MB = ONE_KB * ONE_KB;
    /**
     * The number of bytes in a gigabyte.
     */
    public static final long ONE_GB = ONE_KB * ONE_MB;

    public static String byteCountToDisplaySize(long size) {
        String displaySize;

        if (size / ONE_GB > 0) {
            displaySize = String.format("%1$.1f GB", (float) size / ONE_GB);
        } else if (size / ONE_MB > 0) {
            displaySize = String.format("%1$.1f MB", (float) size / ONE_MB);
        } else if (size / ONE_KB > 0) {
            displaySize = String.format("%1$.1f KB", (float) size / ONE_KB);
        } else {
            displaySize = String.valueOf(size) + " Bytes";
        }
        return displaySize;
    }
}

Related

  1. humanReadableByteCount(long bytes)
  2. humanReadableByteCount(long bytes, boolean size)
  3. sizeConvert(long size)
  4. prettyBytes(long value)