Java Long Number Readable Format bytesToString(long bytes)

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

Description

Convert a number of bytes into a human readable string

License

Open Source License

Parameter

Parameter Description
bytes Numeric bytes (ex: 123899381)

Return

Human readable version of bytes (ex: 1.4 GB)

Declaration

public static String bytesToString(long bytes) 

Method Source Code

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

import java.text.DecimalFormat;

public class Main {
    /**//from  w w  w  .j  av a2  s.  c  om
     * Convert a number of bytes into a human readable string
     * @param bytes Numeric bytes (ex: 123899381)
     * @return Human readable version of bytes (ex: 1.4 GB)
     */
    public static String bytesToString(long bytes) {
        if (bytes > 1024 * 1024 * 1024) {
            return new DecimalFormat("#.##").format((double) bytes / (1024.0 * 1024.0 * 1024.0)) + " GB";
        }
        if (bytes > 1024 * 1024) {
            return new DecimalFormat("#.##").format((double) bytes / (1024.0 * 1024.0)) + " MB";
        }
        if (bytes > 1024) {
            return new DecimalFormat("#.##").format((double) bytes / 1024.0) + " KB";
        }
        return bytes + " B";
    }
}

Related

  1. bytesToHumanString(long bytes)
  2. convertBytes(long bytes)
  3. convertBytes(long size)
  4. convertByteToGMKB(long bytes)
  5. convertByteUnit(Long l)