File Sizes Explained: Bytes, KB, MB, GB, TB — The Developer Guide
Understand file size units — the difference between kilobytes and kibibytes, decimal vs binary prefixes, and how operating systems calculate and display file sizes.
One of the most frequent sources of bugs in file validation is the confusion between decimal (SI, base-10) and binary (IEC, base-2) size units. Knowing whether your server or cloud provider calculates 1MB as 1,000,000 bytes or 1,048,576 bytes prevents subtle boundary failures.
Decimal (SI) vs Binary (IEC) Prefixes
| Unit | Prefix | Base-10 (Decimal / Hard Drives / S3) | Binary Prefix | Base-2 (RAM / OS / Windows) |
|---|---|---|---|---|
| Kilo | KB | 1,000 bytes (10^3) | KiB | 1,024 bytes (2^10) |
| Mega | MB | 1,000,000 bytes (10^6) | MiB | 1,048,576 bytes (2^20) |
| Giga | GB | 1,000,000,000 bytes (10^9) | GiB | 1,073,741,824 bytes (2^30) |
Download our exact 10MB (10,485,760 bytes) PDF test file to test boundary limits in your validator.
Download 10MB PDF Sample →Accurate File Size Formatter in TypeScript
export function formatBytes(bytes: number, decimals = 2, useBinary = true): string {
if (bytes === 0) return '0 Bytes';
const k = useBinary ? 1024 : 1000;
const dm = decimals < 0 ? 0 : decimals;
const sizes = useBinary
? ['Bytes', 'KiB', 'MiB', 'GiB', 'TiB']
: ['Bytes', 'KB', 'MB', 'GB', 'TB'];
const i = Math.floor(Math.log(bytes) / Math.log(k));
return `${parseFloat((bytes / Math.pow(k, i)).toFixed(dm))} ${sizes[i]}`;
}
// Example outputs:
// formatBytes(1048576, 2, true) => "1 MiB"
// formatBytes(1000000, 2, false) => "1 MB"
Practical Advice for File Upload Rules
When configuring upload limits in web applications, explicitly use raw bytes in your configuration schemas (e.g. maxSizeBytes: 10485760) rather than converting strings dynamically.
Nguyen Dai Long
AuthorBackend Lead • Distributed Systems & Cloud Edge Architecture Specialist
4+ years designing high-throughput file ingestion pipelines, database architectures, and distributed edge storage on Cloudflare R2 & AWS S3. Founder of FileDummy and the NDL Ecosystem.
Was this article helpful?
Click Like to support the author and help other developers discover this guide.
Engineering Discussion & Feedback (0)
Share benchmark results, report edge cases, or ask technical questions.
No comments yet. Be the first developer to start the discussion!
Frequently Asked Questions
Why does Windows show a different file size than the actual size?▾
Windows Explorer shows size on disk (rounded up to the cluster size, typically 4096 bytes) alongside the actual file size. A 1-byte file has 1 byte size but 4 KB size on disk. Also, Windows uses 1 KB = 1,024 bytes while storage manufacturers use 1 KB = 1,000 bytes.
What is the difference between KB and KiB?▾
1 KB (kilobyte, SI standard) = 1,000 bytes. 1 KiB (kibibyte, IEC standard) = 1,024 bytes. Operating systems historically use KB to mean 1,024 bytes (binary), causing ambiguity. IEC introduced KiB/MiB/GiB for clarity. In programming, always be explicit about which base you are using.
How many bytes is 1 GB exactly?▾
Decimal (storage manufacturers): 1 GB = 1,000,000,000 bytes. Binary (OS convention): 1 GiB = 1,073,741,824 bytes. This 7.4% difference explains why a 1TB hard drive shows as 931 GB in Windows. FileDummy.dev uses binary sizes for all files.