DEV Community

Cover image for I Built a File Size Converter in Vanilla JS - and Fell Into the 1000 vs 1024 Rabbit Hole
Muhammad Ameen Khan
Muhammad Ameen Khan

Posted on

I Built a File Size Converter in Vanilla JS - and Fell Into the 1000 vs 1024 Rabbit Hole

A file size converter sounds like the easiest tool in the world. Divide by a thousand, or by 1024, print the answer, done.

Then a user buys a 500 GB drive, plugs it in, and Windows tells them it's 465 GB. Forty-five gigabytes, apparently stolen. That's the moment you learn this tool is not a math problem - it's an argument the entire tech industry has been having for decades, and your converter has to pick a side.

So I built a free file size converter for CalcVersa that converts between bytes, KB, MB, GB and TB. No framework, no backend - one HTML file with vanilla JS that runs entirely in the browser. Here's the one design decision that mattered more than all the code.

How many bytes are in a kilobyte? There are two correct answers

System 1 KB Who uses it
Decimal (SI) 1,000 bytes Hard drive & SSD manufacturers, macOS
Binary 1,024 bytes Windows, RAM, most programming contexts

The binary system even has its own official names - KiB, MiB, GiB-
created precisely to end this confusion. Almost nobody uses them in a user interface, so the confusion survives.

Now the "stolen" drive space:

500 GB as sold:      500 × 1000³ = 500,000,000,000 bytes
Windows reads it as: 500,000,000,000 ÷ 1024³ = 465.66
Enter fullscreen mode Exit fullscreen mode

The drive maker counted in decimal and labelled it GB. Windows counts in binary and also labels it GB. Nobody lied and nothing is missing - the same bytes, measured with two different rulers, wearing the same name tag.

The code: normalise everything to bytes

Whatever the user types, in whatever unit, the converter turns it into bytes first - one source of truth - and then divides back out into the target unit:

var FACTORS = {
  bytes: 1,
  kb: 1024,
  mb: 1024 ** 2,
  gb: 1024 ** 3,
  tb: 1024 ** 4
};

function convert(value, from, to) {
  var bytes = value * FACTORS[from];
  return bytes / FACTORS[to];
}
Enter fullscreen mode Exit fullscreen mode

Every conversion is the same two operations, so KB→MB and B→TB can never disagree with each other. Adding a unit means adding one line to the table, not touching the logic.

Picking the right unit automatically for display uses the same idea - a logarithm is just asking "how many times can I divide this by 1024?":

function formatBytes(bytes, decimals) {
  if (!bytes) return '0 B';
  var units = ['B', 'KB', 'MB', 'GB', 'TB'];
  var i = Math.floor(Math.log(bytes) / Math.log(1024));
  i = Math.min(i, units.length - 1);
  return parseFloat((bytes / 1024 ** i).toFixed(decimals)) + ' ' + units[i];
}
Enter fullscreen mode Exit fullscreen mode

The parseFloat around toFixed is deliberate: it trims the trailing zeros, so you show 1.5 MB instead of 1.50 MB.

The decision I actually had to make

The code took an afternoon. Choosing the base took longer.

I shipped binary (1024), because that's the number my users already see everywhere else - in Windows Explorer, in their code, in every other converter they compare mine against. A converter that answers in decimal gets reported as buggy, even when it's technically following the SI standard.

But the page still explains the decimal system next to the tool, because the second most common question - "where did my drive space go?" - is a decimal question. The lesson generalises: for a unit converter, there is no universally right answer. There's only the answer your user expects, clearly labelled.

What I deliberately kept boring

  • No framework. The whole tool is one self-contained block, so it can be pasted into any page builder and just work.
  • No rounding drift. Everything routes through bytes, so converting A→B→A returns exactly where you started.
  • Everything client-side. Nothing typed into the converter ever leaves the browser.

Try it

The converter is live here: File Size Converter - type a value in any unit and it shows the full row of results, so you can see both the size you have and the size your operating system thinks you have.

If you write converters: decimal or binary - which base does your code use, and has a user ever reported the other one as a bug?

Top comments (0)