@Test public void should_be_possible_to_show_negative_kikibytes_in_human_readable_form(){ assertThat(humanReadableByteCount(NEGATIVE_ONE_KIKIBYTE, false), is("- 1.00 KiB")); }
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(); }
NumberConversion { 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(); } }
NumberConversion { 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(); }  }
NumberConversion { 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(); }  static String humanReadableByteCount(Long bytes, boolean decimal); static long convertToLong(Object number); }
NumberConversion { 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(); }  static String humanReadableByteCount(Long bytes, boolean decimal); static long convertToLong(Object number);  }