Java Size Format formatFileSize(long size)

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

Description

format File Size

License

Open Source License

Declaration

public static String formatFileSize(long size) 

Method Source Code

//package com.java2s;
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
 *
 * Copyright (C) 2008 IVER T.I. and Generalitat Valenciana.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
 *///from w w  w .  j a va 2s .c  o m

import java.text.NumberFormat;

public class Main {

    public static String formatFileSize(long size) {
        double bytes = size;
        double kBytes = 0.0;
        double mBytes = 0.0;
        double gBytes = 0.0;
        if (bytes >= 1024.0) {
            kBytes = bytes / 1024.0;
            if (kBytes >= 1024.0) {
                mBytes = kBytes / 1024.0;
                if (mBytes >= 1024.0)
                    gBytes = mBytes / 1024.0;
            }
        }

        String texto = "";
        NumberFormat numberFormat = NumberFormat.getNumberInstance();
        numberFormat.setMinimumFractionDigits(0);

        do {
            if (gBytes > 0) {
                numberFormat.setMaximumFractionDigits(2 - (int) Math.log10(gBytes));
                texto = numberFormat.format(gBytes) + " GB";
                break;
            }
            if (mBytes > 0) {
                numberFormat.setMaximumFractionDigits(2 - (int) Math.log10(mBytes));
                texto = numberFormat.format(mBytes) + " MB";
                break;
            }
            if (kBytes > 0) {
                numberFormat.setMaximumFractionDigits(2 - (int) Math.log10(kBytes));
                texto = numberFormat.format(kBytes) + " KB";
                break;
            }
            if (bytes != 0) {
                numberFormat.setMaximumFractionDigits(0);
                texto = numberFormat.format(bytes) + " bytes";
                break;
            }
        } while (false);

        numberFormat.setMaximumFractionDigits(0);

        return texto + " (" + numberFormat.format(bytes) + " bytes)";
    }
}

Related

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