Java File Size Readable 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;
//License from project: Open Source License 

public class Main {
    public static final long ONE_KILOBYTE = 1000;
    public static final long ONE_MEGABYTE = 1000 * ONE_KILOBYTE;
    public static final long ONE_GIGABYTE = 1000 * ONE_MEGABYTE;

    public static String formatSize(long size) {
        if (size >= 100 * ONE_GIGABYTE)
            return String.format("%,d GB", size / ONE_GIGABYTE);
        if (size >= 10 * ONE_GIGABYTE)
            return String.format("%.1f GB", (double) size / ONE_GIGABYTE);
        if (size >= ONE_GIGABYTE)
            return String.format("%.2f GB", (double) size / ONE_GIGABYTE);
        if (size >= 10 * ONE_MEGABYTE)
            return String.format("%,d MB", size / ONE_MEGABYTE);
        if (size >= ONE_MEGABYTE)
            return String.format("%.1f MB", (double) size / ONE_MEGABYTE);
        if (size >= ONE_KILOBYTE)
            return String.format("%,d KB", size / ONE_KILOBYTE);

        return String.format("%,d bytes", size);
    }//w  w  w .  ja v  a  2 s.  c om
}

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 size)