Java Long Number Readable Format humanReadableSize(long byteCount)

Here you can find the source of humanReadableSize(long byteCount)

Description

Returns a human-readable (memory, file, ...) size.

License

Apache License

Parameter

Parameter Description
byteCount the number of bytes

Return

a human-readable size with units

Declaration

public static String humanReadableSize(long byteCount) 

Method Source Code

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

public class Main {
    /** Number of bytes in a kilobyte. */
    public static final int KB_BYTES = 1024;
    /** Number of bytes in a megabyte. */
    public static final long MB_BYTES = 1024 * 1024;
    /** Number of bytes in a gigabyte. */
    public static final long GB_BYTES = 1024 * 1024 * 1024;

    /**//w w  w .  j  a v a 2s  . c  om
     * Returns a human-readable (memory, file, ...) size.
     * @param byteCount the number of bytes
     * @return a human-readable size with units
     */
    public static String humanReadableSize(long byteCount) {
        String result;
        if (byteCount / GB_BYTES > 0) {
            result = Long.toString(byteCount / GB_BYTES) + " GB";
        } else if (byteCount / MB_BYTES > 0) {
            result = Long.toString(byteCount / MB_BYTES) + " MB";
        } else if (byteCount / KB_BYTES > 0) {
            result = Long.toString(byteCount / KB_BYTES) + " kB";
        } else {
            result = Long.toString(byteCount) + " B";
        }
        return result;
    }
}

Related

  1. humanReadableBytes(long bytes)
  2. humanReadableBytes(long bytes)
  3. humanReadableDateDiff(long start, long end)
  4. humanReadableDuration(long length)
  5. humanReadableDuration(long ms)
  6. humanReadableSize(long bytes)
  7. humanReadableTime(final long duration)
  8. humanTimeDiff(long timeDiff)
  9. toHuman(long amount)