Java File Size Readable Format convertFileSize(long size)

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

Description

convert File Size

License

Open Source License

Declaration

public static String convertFileSize(long size) 

Method Source Code

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

import java.text.DecimalFormat;

public class Main {
    public static String convertFileSize(long size) {
        long kb = 1024;
        long mb = kb * 1024;
        long gb = mb * 1024;
        DecimalFormat df = new DecimalFormat("#.00");
        if (size >= gb) {
            return df.format((double) size / gb) + "GB";
        } else if (size >= mb) {
            return df.format((double) size / mb) + "MB";
        } else if (size >= kb) {
            return df.format((double) size / kb) + "KB";
        } else//from  w  ww  .ja  v  a2  s .  c  om
            return df.format((double) size) + "B";
    }
}

Related

  1. convertFileSize(double size, Locale locale)
  2. convertFileSize(long size)
  3. formatarNumeroComZerosAEsquerda(long numero, int size)
  4. formatByteSize(long bytes, int decimals)
  5. formatByteSize(long bytes, int lastDigits)
  6. formatByteSize(long byteSize)