Last updated
Digital Storage Units
Digital storage is measured in bytes and their multiples. There are two systems: decimal (SI) prefixes (kilo = 1000) used by storage manufacturers and network speeds, and binary (IEC) prefixes (kibi = 1024) used by operating systems for file sizes. This discrepancy is why a "1TB" hard drive shows as ~931 GiB in Windows.
Storage Unit Reference
| Decimal (SI) | Value | Binary (IEC) | Value |
|---|---|---|---|
| Kilobyte (KB) | 1,000 B | Kibibyte (KiB) | 1,024 B |
| Megabyte (MB) | 1,000,000 B | Mebibyte (MiB) | 1,048,576 B |
| Gigabyte (GB) | 1,000,000,000 B | Gibibyte (GiB) | 1,073,741,824 B |
| Terabyte (TB) | 10¹² B | Tebibyte (TiB) | 2⁴⁰ B |
| Petabyte (PB) | 10¹⁵ B | Pebibyte (PiB) | 2⁵⁰ B |
Conversion in JavaScript
JavaScript
const UNITS_DECIMAL = ['B','KB','MB','GB','TB','PB'];
const UNITS_BINARY = ['B','KiB','MiB','GiB','TiB','PiB'];
function formatBytes(bytes, binary = false) {
if (bytes === 0) return '0 B';
const base = binary ? 1024 : 1000;
const units = binary ? UNITS_BINARY : UNITS_DECIMAL;
const i = Math.floor(Math.log(bytes) / Math.log(base));
return (bytes / Math.pow(base, i)).toFixed(2) + ' ' + units[i];
}
console.log(formatBytes(1073741824)); // '1.07 GB'
console.log(formatBytes(1073741824, true)); // '1.00 GiB'
console.log(formatBytes(1500000000)); // '1.50 GB'