Java Long Number Readable Format humanReadableByteCount(Long bytes, boolean decimal)

Here you can find the source of humanReadableByteCount(Long bytes, boolean decimal)

Description

Takes the given bytes and transforms them into a human readable format

License

Open Source License

Parameter

Parameter Description
bytes Bytes to be transformed
decimal If the number is in decimal

Return

The transformed bytes in human readable format

Declaration

public static String humanReadableByteCount(Long bytes, boolean decimal) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    /**/*from ww w  .ja  va2 s. c o m*/
     * Takes the given bytes and transforms them into a human readable format
     *
     * @param bytes Bytes to be transformed
     * @param decimal If the number is in decimal
     * @return The transformed bytes in human readable format
     */
    public static String humanReadableByteCount(Long bytes, boolean decimal) {
        if (bytes == null)
            bytes = 0L;
        String sign = bytes < 0 ? "-" : "";
        Long absBytes = Math.abs(bytes);
        int unit = decimal ? 1000 : 1024;
        if (absBytes < unit) {
            return sign + absBytes + " B";
        }
        int exp = (int) (Math.log(absBytes) / Math.log(unit));
        String pre = (decimal ? "kMGTPE" : "KMGTPE").charAt(exp - 1) + (decimal ? "" : "i");

        return String.format("%s %.2f %sB", sign, absBytes / Math.pow(unit, exp), pre).trim();
    }
}

Related

  1. humanReadable(long memory)
  2. humanReadable(long number)
  3. humanReadableByteCount(final long bytes)
  4. humanReadableByteCount(final long bytes, final boolean si)
  5. humanReadableByteCount(long bytes)
  6. humanReadableByteCount(long bytes, boolean si)
  7. humanReadableByteCount(long bytes, boolean si)
  8. humanReadableByteCount(long bytes, boolean si)
  9. humanReadableBytes(long bytes)