Java File Size Readable Format formatSize(long size)

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

Description

format Size

License

LGPL

Declaration

public static String formatSize(long size) 

Method Source Code

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

public class Main {

    public static String formatSize(long size) {
        long SIZE_KB = 1024;
        long SIZE_MB = SIZE_KB * 1024;
        long SIZE_GB = SIZE_MB * 1024;

        if (size < SIZE_KB) {
            return String.format("%d B", (int) size);
        } else if (size < SIZE_MB) {
            return String.format("%.2f KB", (float) size / SIZE_KB);
        } else if (size < SIZE_GB) {
            return String.format("%.2f MB", (float) size / SIZE_MB);
        } else {/*from  w ww . j  a v a 2s .c o m*/
            return String.format("%.2f GB", (float) size / SIZE_GB);
        }
    }
}

Related

  1. formatSize(long size)
  2. formatSize(long size)
  3. formatSize(long size)
  4. formatSize(long size)
  5. formatSize(long size)
  6. formatSize(long size)
  7. formatSize(Long size)
  8. formatSize(long size)
  9. formatSize(long v)