Java Size Format formatFilesize(int s)

Here you can find the source of formatFilesize(int s)

Description

Format file size

License

Open Source License

Parameter

Parameter Description
s file size

Return

file size as string

Declaration

public static String formatFilesize(int s) 

Method Source Code


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

import java.text.NumberFormat;

public class Main {
    /**/*from w w w  . j a  va  2  s.  c  o m*/
     * Format file size
     * @param s file size
     * @return file size as string
     */
    public static String formatFilesize(int s) {
        if (s < 1024) {
            return s + "B";
        } else {
            NumberFormat nf = NumberFormat.getInstance();

            nf.setMaximumFractionDigits(1);
            nf.setMinimumFractionDigits(1);

            if (s < (1024 * 1024)) {
                return nf.format((double) s / 1024) + "KB";
            } else if (s < (1024 * 1024 * 1024)) {
                return nf.format((double) s / 1024 / 1024) + "MB";
            } else if (s < (1024 * 1024 * 1024 * 1024)) {
                return nf.format((double) s / 1024 / 1024 / 1024) + "GB";
            } else {
                return nf.format((double) s / 1024 / 1024 / 1024 / 1024) + "TB";
            }
        }
    }
}

Related

  1. formatDecimal(double size)
  2. formatDiskSize(final long diskSize)
  3. formatFileSize(double fileSize, int precision, int unit, boolean showUnit)
  4. formatFileSize(File file)
  5. formatFilesize(final long filesize, final Locale locale)
  6. formatFileSize(long fileS)
  7. formatFilesize(long filesize)
  8. formatFileSize(long fileSize, int decimalPos)
  9. formatFileSize(long length)