Java Size Format formatSize(long longSize, int decimalPos)

Here you can find the source of formatSize(long longSize, int decimalPos)

Description

Formats a file size

License

Open Source License

Parameter

Parameter Description
longSize a parameter
decimalPos a parameter

Return

formatted string for size.

Declaration

public static String formatSize(long longSize, int decimalPos) 

Method Source Code

//package com.java2s;
/*/*  w ww  . j  a v a2 s  .  co  m*/
 * Copyright (c) 2012 Diamond Light Source Ltd.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 */

import java.text.NumberFormat;

public class Main {
    /**
     * Formats a file size
     * 
     * @param longSize
     * @param decimalPos
     * @return formatted string for size.
     */
    public static String formatSize(long longSize, int decimalPos) {
        NumberFormat fmt = NumberFormat.getNumberInstance();
        if (decimalPos >= 0) {
            fmt.setMaximumFractionDigits(decimalPos);
        }
        final double size = longSize;
        double val = size / (1024 * 1024 * 1024);
        if (val > 1) {
            return fmt.format(val).concat(" GB");
        }
        val = size / (1024 * 1024);
        if (val > 1) {
            return fmt.format(val).concat(" MB");
        }
        val = size / 1024;
        if (val > 10) {
            return fmt.format(val).concat(" KB");
        }
        return fmt.format(size).concat(" bytes");
    }
}

Related

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