Here you can find the source of humanReadableByteCount(Long bytes, boolean decimal)
Parameter | Description |
---|---|
bytes | Bytes to be transformed |
decimal | If the number is in decimal |
public static String humanReadableByteCount(Long bytes, boolean decimal)
//package com.java2s; //License from project: Open Source License public class Main { /**/*from ww w .ja va2 s. c o m*/ * Takes the given bytes and transforms them into a human readable format * * @param bytes Bytes to be transformed * @param decimal If the number is in decimal * @return The transformed bytes in human readable format */ public static String humanReadableByteCount(Long bytes, boolean decimal) { if (bytes == null) bytes = 0L; String sign = bytes < 0 ? "-" : ""; Long absBytes = Math.abs(bytes); int unit = decimal ? 1000 : 1024; if (absBytes < unit) { return sign + absBytes + " B"; } int exp = (int) (Math.log(absBytes) / Math.log(unit)); String pre = (decimal ? "kMGTPE" : "KMGTPE").charAt(exp - 1) + (decimal ? "" : "i"); return String.format("%s %.2f %sB", sign, absBytes / Math.pow(unit, exp), pre).trim(); } }