Format bytes into a rounded string representation using IEC standard (matches Mac/Linux). For hard drive capacities, use @link #formatBytesDecimal(long). For Windows displays for KB, MB and GB, in JEDEC units, edit the returned string to remove the 'i' to display the (incorrect) JEDEC units.
(long bytes)
| 80 | * @return Rounded string representation of the byte size. |
| 81 | */ |
| 82 | public static String formatBytes(long bytes) { |
| 83 | if (bytes == 1L) { // bytes |
| 84 | return String.format("%d byte", bytes); |
| 85 | } else if (bytes < KIBI) { // bytes |
| 86 | return String.format("%d bytes", bytes); |
| 87 | } else if (bytes < MEBI) { // KiB |
| 88 | return formatUnits(bytes, KIBI, "KiB"); |
| 89 | } else if (bytes < GIBI) { // MiB |
| 90 | return formatUnits(bytes, MEBI, "MiB"); |
| 91 | } else if (bytes < TEBI) { // GiB |
| 92 | return formatUnits(bytes, GIBI, "GiB"); |
| 93 | } else if (bytes < PEBI) { // TiB |
| 94 | return formatUnits(bytes, TEBI, "TiB"); |
| 95 | } else if (bytes < EXBI) { // PiB |
| 96 | return formatUnits(bytes, PEBI, "PiB"); |
| 97 | } else { // EiB |
| 98 | return formatUnits(bytes, EXBI, "EiB"); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Format units as exact integer or fractional decimal based on the prefix, |