Java Long Number Readable Format humanReadableBytes(long bytes)

Here you can find the source of humanReadableBytes(long bytes)

Description

Format the provided integer parameter in units of bytes as a human-readable string.

License

Open Source License

Parameter

Parameter Description
bytes The number to make a human readable units string from.

Return

human readable string representation of the number of bytes

Declaration

public static String humanReadableBytes(long bytes) 

Method Source Code

//package com.java2s;

public class Main {
    /**/*from w w  w. j a va2  s . co m*/
     * SI unit multiples for Byte - increases by a factor of 10^3. Only required
     * up to max long 2^61
     */
    private static final String[] BYTE_UNITS_SUFFIX_SI = { "", "k", "M", "G", "T", "P" };
    /**
     * IEC unit multiples for Byte - increases by a factor of 2^10. Only
     * required up to max long 2^61
     */
    private static final String[] BYTE_UNITS_SUFFIX_IEC = { "", "Ki", "Mi", "Gi", "Ti", "Pi" };

    /**
     * Format the provided integer parameter in units of bytes as a
     * human-readable string. For example 2048 produces become "2KiB". Uses
     * non-SI (1024 base) EIC units.
     *
     * @param bytes The number to make a human readable units string from.
     * @return human readable string representation of the number of bytes
     */
    public static String humanReadableBytes(long bytes) {
        return humanReadableBytes(bytes, false, true);
    }

    /**
     * Format the provided integer parameter in units of bytes as a
     * human-readable string. For example 2048 produces become "2KiB". <p> The
     * output can be in either SI base units (multiples of 1000) or the more
     * traditional multiples of 1024 (2<sup>10</sup>). In the case that
     * multiples of 1024 are used, the correct IEC units infix an i character.
     * For example using KiB for kilobyte instead of kB. </p>
     *
     * @param bytes The number to make a human readable units string from.
     * @param si whether to not to use SI (1000) units, use 1024 otherwise
     * @param iec whether or not use IEC units (e.g KiB)
     * @return human readable string representation of the number of bytes
     */
    public static String humanReadableBytes(long bytes, boolean si, boolean iec) {

        // Record and strip the sign
        final int signum = Long.signum(bytes);
        final long posBytes = bytes * signum;

        // Calculate the power, for either base 1000 or 1024
        final double byteFrac;
        final String unit;
        long order = 0;
        if (si) {

            long lim = 1000L;
            while (order < BYTE_UNITS_SUFFIX_SI.length && posBytes >= lim) {
                lim *= 1000L;
                order++;
            }
            byteFrac = posBytes / (double) (lim / 1000L);
            unit = BYTE_UNITS_SUFFIX_SI[(int) order];

        } else {

            final String[] units = iec ? BYTE_UNITS_SUFFIX_IEC : BYTE_UNITS_SUFFIX_SI;
            while (order < units.length && posBytes >= 1L << 10L * (order + 1L)) {
                order++;
            }
            byteFrac = posBytes / (double) (1L << (10L * order));
            unit = units[(int) order];
        }

        return String.format(Math.floor(byteFrac) == byteFrac ? "%s%.0f%sB" : "%s%.1f%sB", signum == -1 ? "-" : "",
                byteFrac, unit);
    }
}

Related

  1. humanReadableByteCount(long bytes)
  2. humanReadableByteCount(Long bytes, boolean decimal)
  3. humanReadableByteCount(long bytes, boolean si)
  4. humanReadableByteCount(long bytes, boolean si)
  5. humanReadableByteCount(long bytes, boolean si)
  6. humanReadableBytes(long bytes)
  7. humanReadableDateDiff(long start, long end)
  8. humanReadableDuration(long length)
  9. humanReadableDuration(long ms)