Java Number Format Pattern formatFileLength(long length)

Here you can find the source of formatFileLength(long length)

Description

Returns a nicer representation of the number as a file length.

License

Open Source License

Parameter

Parameter Description
length the number<i>(file length or whatever)</i> to be formatted.

Return

the nicely formatted number as a String.

Declaration

public static String formatFileLength(long length) 

Method Source Code

//package com.java2s;
/*// w  w  w  .j  a v  a 2 s  .  c o  m
 * Copyright 2004-2009 Luciano Vernaschi
 *
 * This file is part of MeshCMS.
 *
 * MeshCMS 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.
 *
 * MeshCMS 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 MeshCMS.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.io.File;

import java.text.DecimalFormat;

public class Main {
    /**
     * The number of bytes in a kilobyte (2^10).
     */
    public static final int KBYTE = 1024;
    /**
     * The number of bytes in a megabyte (2^20).
     */
    public static final int MBYTE = KBYTE * KBYTE;
    /**
     * The number of bytes in a gigabyte (2^30).
     */
    public static final int GBYTE = MBYTE * KBYTE;

    /**
     * Returns a nicer representation of the length of the file. The file length
     * is returned as bytes, kilobytes or megabytes, with the unit attached.
     * @see #formatFileLength(long)
     *
     * @param file the File
     *
     * @return the nicely formatted length of this file
     */
    public static String formatFileLength(File file) {
        return formatFileLength(file.length());
    }

    /**
     * Returns a nicer representation of the number as a file length. The number
     * is returned as bytes, kilobytes or megabytes, with the unit attached.
     *
     * @param length the number<i>(file length or whatever)</i> to be formatted.
     *
     * @return the nicely formatted number as a String.
     */
    public static String formatFileLength(long length) {
        DecimalFormat format = new DecimalFormat("###0.##");
        double num = length;
        String unit;

        if (length < KBYTE) {
            unit = "B";
        } else if (length < MBYTE) {
            num /= KBYTE;
            unit = "KB";
        } else if (length < GBYTE) {
            num /= MBYTE;
            unit = "MB";
        } else {
            num /= GBYTE;
            unit = "GB";
        }

        return format.format(num) + unit;
    }
}

Related

  1. formatBytes(long numBytes)
  2. formatDashboardNumber(Number amount)
  3. formatDisplay(String displayString)
  4. formatDollar(Object obj)
  5. formatDollarTd(Object obj)
  6. formatGopNumber(Number gop)
  7. formatI18N(Object ob)
  8. formatiereSpeichergroesse(long bytes)
  9. formatIncludeCommas(final Number object)