@Test @DisplayName("Format byte counts in SI system") void formatByteCountSI_1() { assertEquals("0 B", formatByteCountSI(0L)); assertEquals("27 B", formatByteCountSI(27L)); assertEquals("999 B", formatByteCountSI(999L)); assertEquals("1.0 kB", formatByteCountSI(1000L)); assertEquals("1.0 kB", formatByteCountSI(1023L)); assertEquals("1.0 kB", formatByteCountSI(1024L)); assertEquals("1.7 kB", formatByteCountSI(1728L)); assertEquals("110.6 kB", formatByteCountSI(110592L)); assertEquals("7.1 MB", formatByteCountSI(7077888L)); assertEquals("453.0 MB", formatByteCountSI(452984832L)); assertEquals("29.0 GB", formatByteCountSI(28991029248L)); assertEquals("1.9 TB", formatByteCountSI(1855425871872L)); assertEquals("9.2 EB", formatByteCountSI(9223372036854775807L)); }
public static String formatByteCountSI(long bytes) { long b = bytes == Long.MIN_VALUE ? Long.MAX_VALUE : Math.abs(bytes); return b < 1000L ? bytes + " B" : b < 999_950L ? String.format("%.1f kB", b / 1e3) : (b /= 1000) < 999_950L ? String.format("%.1f MB", b / 1e3) : (b /= 1000) < 999_950L ? String.format("%.1f GB", b / 1e3) : (b /= 1000) < 999_950L ? String.format("%.1f TB", b / 1e3) : (b /= 1000) < 999_950L ? String.format("%.1f PB", b / 1e3) : String.format("%.1f EB", b / 1e6); }
TextUtil { public static String formatByteCountSI(long bytes) { long b = bytes == Long.MIN_VALUE ? Long.MAX_VALUE : Math.abs(bytes); return b < 1000L ? bytes + " B" : b < 999_950L ? String.format("%.1f kB", b / 1e3) : (b /= 1000) < 999_950L ? String.format("%.1f MB", b / 1e3) : (b /= 1000) < 999_950L ? String.format("%.1f GB", b / 1e3) : (b /= 1000) < 999_950L ? String.format("%.1f TB", b / 1e3) : (b /= 1000) < 999_950L ? String.format("%.1f PB", b / 1e3) : String.format("%.1f EB", b / 1e6); } }
TextUtil { public static String formatByteCountSI(long bytes) { long b = bytes == Long.MIN_VALUE ? Long.MAX_VALUE : Math.abs(bytes); return b < 1000L ? bytes + " B" : b < 999_950L ? String.format("%.1f kB", b / 1e3) : (b /= 1000) < 999_950L ? String.format("%.1f MB", b / 1e3) : (b /= 1000) < 999_950L ? String.format("%.1f GB", b / 1e3) : (b /= 1000) < 999_950L ? String.format("%.1f TB", b / 1e3) : (b /= 1000) < 999_950L ? String.format("%.1f PB", b / 1e3) : String.format("%.1f EB", b / 1e6); }  }
TextUtil { public static String formatByteCountSI(long bytes) { long b = bytes == Long.MIN_VALUE ? Long.MAX_VALUE : Math.abs(bytes); return b < 1000L ? bytes + " B" : b < 999_950L ? String.format("%.1f kB", b / 1e3) : (b /= 1000) < 999_950L ? String.format("%.1f MB", b / 1e3) : (b /= 1000) < 999_950L ? String.format("%.1f GB", b / 1e3) : (b /= 1000) < 999_950L ? String.format("%.1f TB", b / 1e3) : (b /= 1000) < 999_950L ? String.format("%.1f PB", b / 1e3) : String.format("%.1f EB", b / 1e6); }  static String formatByteCount(long bytes); static String formatByteCountSI(long bytes); static long unformatByteCount(String count); }
TextUtil { public static String formatByteCountSI(long bytes) { long b = bytes == Long.MIN_VALUE ? Long.MAX_VALUE : Math.abs(bytes); return b < 1000L ? bytes + " B" : b < 999_950L ? String.format("%.1f kB", b / 1e3) : (b /= 1000) < 999_950L ? String.format("%.1f MB", b / 1e3) : (b /= 1000) < 999_950L ? String.format("%.1f GB", b / 1e3) : (b /= 1000) < 999_950L ? String.format("%.1f TB", b / 1e3) : (b /= 1000) < 999_950L ? String.format("%.1f PB", b / 1e3) : String.format("%.1f EB", b / 1e6); }  static String formatByteCount(long bytes); static String formatByteCountSI(long bytes); static long unformatByteCount(String count);  }