Java Long Number Readable Format formatBytes(long size)

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

Description

Returns a human-readable version of the file size, where the input represents a specific number of bytes.

License

Open Source License

Parameter

Parameter Description
size the number of bytes

Return

a human-readable display value (includes units)

Declaration

public static String formatBytes(long size) 

Method Source Code

//package com.java2s;
// it under the terms of the GNU General Public License as published by

public class Main {
    public static final long ONE_KB = 1024;
    public static final long ONE_MB = ONE_KB * ONE_KB;
    public static final long ONE_GB = ONE_KB * ONE_MB;

    /**/*from  w  w w.j  av  a2s.c  om*/
     * Returns a human-readable version of the file size, where the input represents a specific number of bytes.
     *
     * @param size the number of bytes
     * @return a human-readable display value (includes units)
     */
    public static String formatBytes(long size) {
        String display;

        if (size / ONE_GB > 0) {
            display = String.format("%.2f", (size + 0.0) / ONE_GB) + " GB";
        } else if (size / ONE_MB > 0) {
            display = String.format("%.2f", (size + 0.0) / ONE_MB) + " MB";
        } else if (size / ONE_KB > 0) {
            display = String.format("%.2f", (size + 0.0) / ONE_KB) + " KB";
        } else {
            display = String.valueOf(size) + " bytes";
        }
        return display;
    }
}

Related

  1. formatBytes(long bytes)
  2. formatBytes(long bytes)
  3. formatBytes(long bytes)
  4. formatBytes(long d)
  5. formatBytes(long l)
  6. getHumanReadable(long ts, boolean addUTC)
  7. getHumanReadableFileSize(Long fileSize)
  8. getHumanReadableIfSpeed(long ifSpeed)
  9. getHumanReadableSize(long bytes)