Java Size Format formatFileSize(long size)

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

Description

create a formatted filesize string

License

Apache License

Declaration

public static String formatFileSize(long size) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.text.DecimalFormat;

public class Main {
    private static final String[] SIZE_UNIT = { " B", " kiB", " MiB", " GiB", " TiB" };
    private static final DecimalFormat FSIZE = new DecimalFormat("0.00");

    /** create a formatted filesize string */
    public static String formatFileSize(long size) {
        float tmpSize = size;
        int idx = 0;
        while (tmpSize > 1024) {
            tmpSize /= 1024.0f;//from  ww  w.  j  a va2  s  .c om
            ++idx;
        }
        return FSIZE.format(tmpSize) + SIZE_UNIT[idx];
    }
}

Related

  1. formatFilesize(int s)
  2. formatFileSize(long fileS)
  3. formatFilesize(long filesize)
  4. formatFileSize(long fileSize, int decimalPos)
  5. formatFileSize(long length)
  6. formatFileSize(long size)
  7. formatFileSize(long size)
  8. formatFileSize(long size)
  9. formatFileSize(long size)