Java Long Number Readable Format humanReadableByteCount(long bytes, boolean si)

Here you can find the source of humanReadableByteCount(long bytes, boolean si)

Description

human Readable Byte Count

License

Open Source License

Declaration

public static String humanReadableByteCount(long bytes, boolean si) 

Method Source Code

//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);
    }
}

Related

  1. humanReadableByteCount(final long bytes)
  2. humanReadableByteCount(final long bytes, final boolean si)
  3. humanReadableByteCount(long bytes)
  4. humanReadableByteCount(Long bytes, boolean decimal)
  5. humanReadableByteCount(long bytes, boolean si)
  6. humanReadableByteCount(long bytes, boolean si)
  7. humanReadableBytes(long bytes)
  8. humanReadableBytes(long bytes)
  9. humanReadableDateDiff(long start, long end)