ByteValue { public static String prettyPrint(final long bytes) { if (bytes < 0) { throw new IllegalArgumentException("Value must be >= 0"); } if (bytes < 1024) { return bytes + " B"; } long value = bytes; final CharacterIterator ci = new StringCharacterIterator("KMGTPE"); for (int i = 40; i >= 0 && bytes > 0xfffccccccccccccL >> i; i -= 10) { value >>= 10; ci.next(); } value *= Long.signum(bytes); return String.format(Locale.US, "%.1f %cB", value / 1024.0, ci.current()); }  static long ofKilobytes(final long value); static long ofMegabytes(final long value); static long ofGigabytes(final long value); static String prettyPrint(final long bytes);  }
@Test public void shouldPrettyPrintByteValue() { final String actual = ByteValue.prettyPrint(input); assertThat(actual).isEqualTo(expected); }