Here you can find the source of humanReadableByteCount(long bytes, boolean si)
public static String humanReadableByteCount(long bytes, boolean si)
//package com.java2s; /**/*from w ww . jav a 2s. com*/ * Created on Oct 21, 2010 * This file is part of JObexFTP 2.0, and it contains parts of OBEX4J. * * JObexFTP is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * JObexFTP 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 Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with JObexFTP. If not, see <http://www.gnu.org/licenses/>. * */ import java.text.DateFormat; import java.text.SimpleDateFormat; public class Main { private static final DateFormat format = SimpleDateFormat.getInstance(); public static String humanReadableByteCount(long bytes, boolean si) { int unit = si ? 1000 : 1024; if (bytes < unit) { return bytes + " B"; } int exp = (int) (Math.log(bytes) / Math.log(unit)); String pre = (si ? "kMGTPE" : "KMGTPE").charAt(exp - 1) + (si ? "" : "i"); return String.format("%.1f %sB", bytes / Math.pow(unit, exp), pre); } }