Method Deck / units

Data size conversion

Converts a digital storage quantity between bit, byte, decimal (KB/MB/GB/TB) and binary (KiB/MiB/GiB/TiB) units per IEC 80000-13.

Published for review. The Founder is checking every card himself; anything he strikes comes down the same hour.

Try it

Inputs

Outputs

Steps

  1. Look up the exact bit-equivalent of from_unit and of to_unit: byte=8 bits; KB/MB/GB/TB are 8*1000^n bits (decimal); KiB/MiB/GiB/TiB are 8*1024^n bits (binary).
  2. Convert the input to bits: bits = value * bits_per(from_unit).
  3. Convert bits to the target unit: result = bits / bits_per(to_unit).
  4. Round result to 6 significant figures.

Constants

Sources

Worked example and test vectors

NameInputsAnswer
worked example{"value":1,"from_unit":"GB","to_unit":"GiB"}0.931323 GiB
test vector: MiB to KB{"value":1,"from_unit":"MiB","to_unit":"KB"}1,048.58 KB
test vector: byte to bit{"value":1,"from_unit":"byte","to_unit":"bit"}8.00000 bit

Published card