Java Long Number Readable Format humanReadableBytes(long bytes)

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

Description

Get bytes in human readable form.

License

Open Source License

Parameter

Parameter Description
bytes the number of bytes

Return

the human readable string representation

Declaration

public static String humanReadableBytes(long bytes) 

Method Source Code

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

public class Main {
    /**//from   w  w  w .  j av a2s  . c om
     * Get bytes in human readable form.
     * @param bytes the number of bytes
     * @return the human readable string representation
     */
    public static String humanReadableBytes(long bytes) {
        int unit = 1024;

        if (bytes < unit) {
            return bytes + " B";
        } else {
            int exp = (int) (Math.log(bytes) / Math.log(unit));
            return String.format("%.1f %sB", bytes / Math.pow(unit, exp),
                    "KMGTPE".charAt(exp - 1));
        }
    }
}

Related

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