Java File Size Readable Format formatByteSize(long byteSize)

Here you can find the source of formatByteSize(long byteSize)

Description

format Byte Size

License

Open Source License

Declaration

public static String formatByteSize(long byteSize) 

Method Source Code

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

public class Main {
    public static String formatByteSize(long byteSize) {
        float size = (float) byteSize;
        String metrics = " b";
        if (size >= 1024) {
            size = size / 1024;/*from   w w w .j av  a2  s .c  om*/
            metrics = " Kb";
        }
        if (size >= 1024) {
            size = size / 1024;
            metrics = " Mb";
        }
        String sizeStr = "" + size;
        int idx = sizeStr.indexOf(".");
        if (idx != -1) {
            if (sizeStr.charAt(idx + 1) != '0') {
                sizeStr = sizeStr.substring(0, idx + 2);
            } else {
                sizeStr = sizeStr.substring(0, idx);
            }
        }
        sizeStr += metrics;
        return sizeStr;
    }
}

Related

  1. convertFileSize(long size)
  2. convertFileSize(long size)
  3. formatarNumeroComZerosAEsquerda(long numero, int size)
  4. formatByteSize(long bytes, int decimals)
  5. formatByteSize(long bytes, int lastDigits)
  6. formatByteSizeToString(long bytes)
  7. formatDataSize(long size)
  8. FormatDataSize(long uBytes)
  9. FormatDataSizeKB(long uBytes)