Java Size Format formatSize(int size)

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

Description

Formats a file size in bytes into a human readable file size, e.g 10,4 MB etc.

License

Apache License

Parameter

Parameter Description
size file size in bytes.

Return

file size with denomination.

Declaration

public static String formatSize(int size) 

Method Source Code

//package com.java2s;
/*/*from  w w w.  j  a va 2  s .  co  m*/
 * Copyright 2009 Kantega AS
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.text.DecimalFormat;

public class Main {
    private static final String[] units = new String[] { "B", "KB", "MB",
            "GB", "TB" };

    /**
     * Formats a file size in bytes into a human readable file size, e.g 10,4 MB etc.
     * @param size file size in bytes.
     * @return file size with denomination.
     */
    public static String formatSize(int size) {
        if (size <= 0)
            return "0";
        int digitGroups = (int) (Math.log10(size) / Math.log10(1024));
        return new DecimalFormat("#,##0.#").format(size
                / Math.pow(1024, digitGroups))
                + " " + units[digitGroups];
    }
}

Related

  1. formatGameSize(int size)
  2. formatMemorySize(long size)
  3. formatPartSize(int size, NumberFormat format)
  4. formatSize(double fileSize)
  5. formatSize(double size)
  6. formatSize(long bytes)
  7. formatSize(long bytes)
  8. formatSize(long bytes)
  9. formatSize(long fileSize)