Java Size Format formatSize(long bytes)

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

Description

format Size

License

Open Source License

Declaration

public static String formatSize(long bytes) 

Method Source Code

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

import java.text.DecimalFormat;

public class Main {
    public static String formatSize(long bytes) {
        if (bytes < 1024)
            return bytes + " B";
        else if (bytes < 1024 * 1024)
            return singleDecimalDigit(bytes / 1024.0) + " KB";
        else if (bytes < 1024 * 1024 * 1024)
            return singleDecimalDigit(bytes / 1024.0 / 1024.0) + " MB";
        else/*from w  w  w .  jav a2 s  .  co m*/
            return singleDecimalDigit(bytes / 1024.0 / 1024.0 / 1024.0)
                    + " GB";
    }

    public static String singleDecimalDigit(double d) {
        return new DecimalFormat("#.#").format(d);
    }
}

Related

  1. formatPartSize(int size, NumberFormat format)
  2. formatSize(double fileSize)
  3. formatSize(double size)
  4. formatSize(int size)
  5. formatSize(long bytes)
  6. formatSize(long bytes)
  7. formatSize(long fileSize)
  8. formatSize(long longSize)
  9. formatSize(long longSize, int decimalPos)