Here you can find the source of formatSize(long longSize, int decimalPos)
Parameter | Description |
---|---|
longSize | a parameter |
decimalPos | a parameter |
public static String formatSize(long longSize, int decimalPos)
//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"); } }