Java Long Number Readable Format convertByteUnit(Long l)

Here you can find the source of convertByteUnit(Long l)

Description

Converts a given number to byte units, i.e.

License

Open Source License

Declaration

public static String convertByteUnit(Long l) 

Method Source Code


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

import java.text.DecimalFormat;

public class Main {
    /**//from   w  w  w.  j a v a  2 s. com
     * Converts a given number to byte units, i.e. KiloByte, MegaByte etc.
     */
    public static String convertByteUnit(Long l) {
        String s = null;
        String[] unit = { "B", "KB", "MB", "GB", "TB" };
        int i = 0;
        if (l < 1024) {
            s = "" + l;
        } else {
            float del = 1024.0f;
            float f = l;

            while (f > del) {
                // System.out.println(i + " " + f);
                i++;
                f /= del;
            }
            // System.out.println(i + " " + f);

            DecimalFormat df = new DecimalFormat("0.00");
            s = df.format(f);
        }

        s += " " + unit[i];

        return s;
    }
}

Related

  1. bytesToHumanString(long bytes)
  2. bytesToString(long bytes)
  3. convertBytes(long bytes)
  4. convertBytes(long size)
  5. convertByteToGMKB(long bytes)
  6. convertHumanSize(long byteSize)
  7. convertLongToMega(long value)
  8. convertMbIntoGb(long megaBytes)
  9. formatBytes(long bts)