Here you can find the source of getSizeString(long number, Locale locale)
public static String getSizeString(long number, Locale locale)
//package com.java2s; /******************************************************************************* * Copyright (c) 2009 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 * /*from w w w . ja va 2 s .co m*/ * 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. * * Contributors: * * Astrient Foundation Inc. * www.astrientfoundation.org * rashid@astrientfoundation.org * Rashid Mayes 2009 *******************************************************************************/ import java.text.NumberFormat; import java.util.Locale; public class Main { public static final double KIB = new Double(Math.pow(2, 10)).intValue(); public static final double MIB = new Double(Math.pow(2, 20)).intValue(); public static final double GIB = new Double(Math.pow(2, 30)).intValue(); public static String getSizeString(long number, Locale locale) { String suffix; double sz; if (number == 0) { return "0"; } else if (number >= GIB) { //gb sz = (number / GIB); suffix = "GB"; } else if (number >= MIB) { //mb sz = (number / MIB); suffix = "MB"; } else if (number >= KIB) { //kb sz = number / KIB; suffix = "KB"; } else { sz = number; suffix = "B"; } NumberFormat nf = NumberFormat.getNumberInstance(locale); nf.setMaximumFractionDigits(2); return nf.format(sz) + suffix; } }