Java Size Format formatSize(long size)

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

Description

format Size

License

Open Source License

Declaration

public static String formatSize(long size) 

Method Source Code


//package com.java2s;
import java.text.DecimalFormat;

public class Main {
    private static final String[] units = { " B", " KB", " MB", " GB", " TB", " PB" };

    public static String formatSize(long size) {
        DecimalFormat df = new DecimalFormat("###,###.#");

        long div = 1;
        long testval = size;
        for (int i = 0; i < 6; i++) {
            if ((testval >>>= 10) == 0) {
                return df.format((double) size / div) + units[i];
            }/*  w ww  . j a  v a2 s  . co m*/
            div <<= 10;
        }
        return size + units[0];
    }
}

Related

  1. formatSize(long longSize)
  2. formatSize(long longSize, int decimalPos)
  3. formatSize(long size)
  4. formatSize(long size)
  5. formatSize(long size)
  6. formatSize(long size)
  7. formatSize(long size, boolean forceFixLen, boolean forceSizeInBytes)
  8. formatSize(long sizeInByte)
  9. formatSize(long sizeInBytes)