Java Size bytesToSizeFormat(long bytes)

Here you can find the source of bytesToSizeFormat(long bytes)

Description

bytes To Size Format

License

Open Source License

Declaration

public static String bytesToSizeFormat(long bytes) 

Method Source Code

//package com.java2s;
/*/* www  .j a  v  a 2 s .  com*/
 * B3P Commons Core is a library with commonly used classes for webapps.
 * Included are clieop3, oai, security, struts, taglibs and other
 * general helper classes and extensions.
 *
 * Copyright 2000 - 2008 B3Partners BV
 * 
 * This file is part of B3P Commons Core.
 * 
 * B3P Commons Core 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 3 of the License, or
 * (at your option) any later version.
 * 
 * B3P Commons Core 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 B3P Commons Core.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.util.Locale;

import java.text.NumberFormat;

public class Main {
    public static String bytesToSizeFormat(long bytes) {
        if (bytes < 1024) {
            return bytes + " B";
        }
        NumberFormat f = NumberFormat.getNumberInstance(new Locale("nl"));
        f.setMaximumFractionDigits(1);
        f.setMinimumFractionDigits(1);
        if (bytes < (1024 * 1024)) {
            return f.format(bytes / 1024.0) + " kB";
        }
        if (bytes < (1024 * 1024 * 1024)) {
            return f.format(bytes / (1024 * 1024.0)) + " MB";
        }
        return f.format(bytes / (1024 * 1024 * 1024.0)) + " GB";
    }
}

Related

  1. byteCountToDisplaySize(long size)
  2. byteCountToDisplaySize(long size)
  3. byteCountToDisplaySize(long size)
  4. byteSizeString(long bytes)
  5. BytesToHRSize(String len)
  6. bytesToString(long sizeInBytes)
  7. bytesToUnits(long size)
  8. calcSize(double s, String type, int div)
  9. createSizeFormat(Locale locale)