Java Size Format formatFileSize(long size)

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

Description

Takes a string with a byte count and converts it into a "nice" representation of size.

License

Open Source License

Parameter

Parameter Description
size size to format

Return

String with the formatted size

Declaration

public static String formatFileSize(long size) 

Method Source Code

//package com.java2s;

import java.text.DecimalFormat;

public class Main {
    /**//from w w  w .  java 2s  .c om
     * Takes a string with a byte count and converts it into a "nice" representation of size.
     * <p/>
     * 124 b <br>
     * 34 KB <br>
     * 12 MB <br>
     * 2 GB
     * <p/>
     * Created on May 281, 2004 by Chuck May
     * @param size size to format
     * @return <code>String</code> with the formatted size
     */
    public static String formatFileSize(long size) {
        if (size < 1024)
            return size + " B";
        else if (size < 1024 * 1024)
            return new DecimalFormat("#.# KB").format((double) size / 1024);
        else if (size < 1024 * 1024 * 1024)
            return new DecimalFormat("#.# MB").format((double) size / 1024 / 1024);

        return new DecimalFormat("#.# GB").format((double) size / 1024 / 1024 / 1024);
    }
}

Related

  1. formatFilesize(long filesize)
  2. formatFileSize(long fileSize, int decimalPos)
  3. formatFileSize(long length)
  4. formatFileSize(long size)
  5. formatFileSize(long size)
  6. formatFileSize(long size)
  7. formatFileSize(long size)
  8. formatFileSize(Long sizeBytes)
  9. formatFilesizeGB(long filesize, int fractionDigits)