NumbersUtil { public static String toReadableSize(long bytes) { if (bytes < KB && bytes >= 0) { return Long.toString(bytes) + " bytes"; } StringBuilder builder = new StringBuilder(); Formatter format = new Formatter(builder, Locale.getDefault()); if (bytes < MB) { format.format("%.2f KB", (float)bytes / (float)KB); } else if (bytes < GB) { format.format("%.2f MB", (float)bytes / (float)MB); } else if (bytes < TB) { format.format("%.2f GB", (float)bytes / (float)GB); } else { format.format("%.4f TB", (float)bytes / (float)TB); } format.close(); return builder.toString(); }  static long parseLong(String s, long defaultValue); static int parseInt(String s, int defaultValue); static float parseFloat(String s, float defaultValue); static long bytesToLong(byte b[]); static long bytesToLong(byte b[], int pos); static String bytesToString(byte[] data, int start, int end); static String toReadableSize(long bytes); static long parseInterval(String str, long defaultValue); static int hash(long value);  }
@Test public void toReadableSize() { Locale.setDefault(Locale.US); assertEquals("1.0000 TB", NumbersUtil.toReadableSize((1024l * 1024l * 1024l * 1024l))); assertEquals("1.00 GB", NumbersUtil.toReadableSize(1024 * 1024 * 1024)); assertEquals("1.00 MB", NumbersUtil.toReadableSize(1024 * 1024)); assertEquals("1.00 KB", NumbersUtil.toReadableSize((1024))); assertEquals("1023 bytes", NumbersUtil.toReadableSize((1023))); }