Java Size Format formatSize(long size)

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

Description

Format the document size for human readers

License

Open Source License

Declaration

public static String formatSize(long size) 

Method Source Code

//package com.java2s;
/**/*from  w  ww.  j  a  va2  s.  co  m*/
 *  OpenKM, Open Document Management System (http://www.openkm.com)
 *  Copyright (c) 2006-2013  Paco Avila & Josep Llort
 *
 *  No bytes were intentionally harmed during the development of this application.
 *
 *  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.,
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

import java.text.DecimalFormat;

public class Main {
    /**
     * Format the document size for human readers
     */
    public static String formatSize(long size) {
        DecimalFormat df = new DecimalFormat("#0.0");
        String str;

        if (size / 1024 < 1) {
            str = size + " B";
        } else if (size / 1048576 < 1) {
            str = df.format(size / 1024.0) + " KB";
        } else if (size / 1073741824 < 1) {
            str = df.format(size / 1048576.0) + " MB";
        } else if (size / 1099511627776L < 1) {
            str = df.format(size / 1073741824.0) + " GB";
        } else if (size / 1125899906842624L < 1) {
            str = df.format(size / 1099511627776.0) + " TB";
        } else {
            str = "BIG";
        }

        return str;
    }
}

Related

  1. formatSize(long bytes)
  2. formatSize(long bytes)
  3. formatSize(long fileSize)
  4. formatSize(long longSize)
  5. formatSize(long longSize, int decimalPos)
  6. formatSize(long size)
  7. formatSize(long size)
  8. formatSize(long size)
  9. formatSize(long size)