跳到内容

File Size Unit Reference

A conversion cheatsheet for B / KB / KiB / MB / MiB / GB / GiB. Useful for code, config, and test design.

SI units (decimal) vs. IEC units (binary)

File size units come in two flavours: the decimal SI units, and the binary IEC units. The most common pitfall — confusing MB with MiB — is the source of many failed boundary-value tests.

SIReadsBytesIECReadsBytesDiff
KBkilobyte1,000KiBkibibyte1,024+2.4%
MBmegabyte1,000,000MiBmebibyte1,048,576+4.86%
GBgigabyte1,000,000,000GiBgibibyte1,073,741,824+7.37%
TBterabyte1,000,000,000,000TiBtebibyte1,099,511,627,776+9.95%

MB → bytes lookup

MB (decimal)BytesMiB (binary)Bytes
0.1 MB100,000 B0.1 MiB104,858 B
0.5 MB500,000 B0.5 MiB524,288 B
1 MB1,000,000 B1 MiB1,048,576 B
2 MB2,000,000 B2 MiB2,097,152 B
5 MB5,000,000 B5 MiB5,242,880 B
10 MB10,000,000 B10 MiB10,485,760 B
20 MB20,000,000 B20 MiB20,971,520 B
25 MB25,000,000 B25 MiB26,214,400 B
50 MB50,000,000 B50 MiB52,428,800 B
100 MB100,000,000 B100 MiB104,857,600 B
256 MB256,000,000 B256 MiB268,435,456 B
512 MB512,000,000 B512 MiB536,870,912 B
1 GB1,000,000,000 B1 GiB1,073,741,824 B

How major services and tools interpret unit suffixes

Service / settingSuffixActual interpretationExample
PHP (php.ini)MMiB (binary)10M = 10,485,760 B
Nginx (client_max_body_size)mMiB (binary)10m = 10,485,760 B
Apache (LimitRequestBody)raw bytesraw bytes10485760 = 10 MiB
Windows Explorershows "MB"computes in MiB (binary)"9.99 MB" = 10,476,544 B
macOS Findershows "MB"uses MB (decimal)"10 MB" = 10,000,000 B
Linux (ls -lh)MMiB (binary)10M = 10 MiB
Gmail attachment limit25 MBMB (decimal)25,000,000 B
AWS S3 consoleMiB shownMiB (binary)as displayed

Conversion code

// JavaScript
const UNITS = {
    // SI (decimal)
    KB: 1_000, MB: 1_000_000, GB: 1_000_000_000, TB: 1_000_000_000_000,
    // IEC (binary)
    KiB: 1_024, MiB: 1_048_576, GiB: 1_073_741_824, TiB: 1_099_511_627_776,
};

const toBytes = (value, unit) => value * UNITS[unit];
const fromBytes = (bytes, unit) => bytes / UNITS[unit];

// 10 MiB → bytes
console.log(toBytes(10, 'MiB')); // → 10485760

// 10,000,000 B → MB
console.log(fromBytes(10_000_000, 'MB')); // → 10
console.log(fromBytes(10_000_000, 'MiB').toFixed(2)); // → 9.54
 1_000, 'MB' => 1_000_000, 'GB' => 1_000_000_000,
        'KiB' => 1_024, 'MiB' => 1_048_576, 'GiB' => 1_073_741_824,
    ];
    return (int)($value * ($units[$unit] ?? 1));
}

echo toBytes(10, 'MiB');  // → 10485760
echo toBytes(25, 'MB');   // → 25000000

Boundary-value test files

DevLab provides byte-precise test files for the most common upload limits. Pick the file that matches the unit interpretation you're testing against.