Unit conversion shows up in developer work more often than you'd expect. An API returns a temperature in Fahrenheit and your user is in Europe. A file size comes back in bytes and you need to display it in megabytes. A distance is in miles and your map SDK wants kilometers. None of these are hard problems, but getting the formula slightly wrong produces subtly broken results that can be annoying to debug.
This is a reference for the conversions that come up most often, with the exact formulas and the traps worth knowing about.
Length
The two systems you'll encounter most are the metric system (meters, kilometers) and the US customary / imperial system (inches, feet, miles).
One inch is exactly 2.54 cm — this is a definition, not a measurement. Everything else in the imperial/metric table follows from it.
Temperature
Temperature is the one conversion that isn't just multiplication — it involves an offset too. This is where most off-by-one bugs live.
Celsius → Fahrenheit: F = C × (9/5) + 32
Fahrenheit → Celsius: C = (F − 32) × (5/9)
Celsius → Kelvin: K = C + 273.15
The classic trap: you multiply without adding the offset, or add the offset in the wrong order.
// Wrong — missing the offset
const f = celsius * 1.8;
// Correct
const f = celsius * (9/5) + 32;
Some useful reference points to sanity-check your results:
| Celsius | Fahrenheit | What it means |
|---|---|---|
| −40 | −40 | The only point where C = F |
| 0 | 32 | Water freezes |
| 20 | 68 | Room temperature |
| 37 | 98.6 | Human body temperature |
| 100 | 212 | Water boils |
If your result doesn't pass a quick sanity check against this table, the formula is wrong somewhere.
Data sizes
Data size conversions are multiplication chains, but there are two competing standards and using the wrong one silently produces wrong answers.
Binary (IEC standard) — what operating systems use:
| Unit | Bytes |
|---|---|
| 1 KiB (kibibyte) | 1,024 |
| 1 MiB (mebibyte) | 1,048,576 |
| 1 GiB (gibibyte) | 1,073,741,824 |
Decimal (SI standard) — what storage manufacturers use:
| Unit | Bytes |
|---|---|
| 1 KB (kilobyte) | 1,000 |
| 1 MB (megabyte) | 1,000,000 |
| 1 GB (gigabyte) | 1,000,000,000 |
This is why a "500 GB" hard drive shows up as ~466 GB in Windows (which reports in GiB but labels it GB). The drive manufacturer uses 1 GB = 10⁹ bytes; the OS uses 1 GB = 2³⁰ bytes. Neither is wrong — they're using different standards.
For developer work:
- Use binary (1 KiB = 1024 B) when working with memory, OS-level file sizes, and buffer sizes
- Use decimal (1 KB = 1000 B) when working with network bandwidth specs or storage product datasheets
Weight
Simpler than temperature — just multiplication.
- 1 pound (lb) = 453.592 grams
- 1 ounce (oz) = 28.3495 grams
- 1 kilogram = 2.20462 pounds
The one to watch: ounce (oz) is a weight unit, but fluid ounce (fl oz) is a volume unit. They're not the same thing and don't convert directly.
Time
Time conversions are exact:
- 1 minute = 60 seconds
- 1 hour = 3,600 seconds
- 1 day = 86,400 seconds
- 1 week = 604,800 seconds
The traps are at the edges: months vary in length (28–31 days), years vary (365 vs 366), and leap seconds exist. For anything involving calendar arithmetic, use a proper date library rather than multiplying seconds.
A related problem: Unix timestamps are in seconds since the Unix epoch (January 1, 1970 UTC), but some APIs and JavaScript's Date.now() return milliseconds. If you see timestamps that are 1000× too large or too small, this is almost always the reason.
When to use a converter vs hardcoding the formula
For one-off conversions during development — checking what a value should be, verifying a formula, exploring a range — a browser-based tool is faster than opening a REPL. The Unit Converter handles length, weight, temperature, data, and time without any setup.
For production code, hardcode the formula you need. Don't call a conversion API for something as simple as fahrenheit = celsius * 1.8 + 32. Pick a well-tested library if you need calendar arithmetic or currency (exchange rates change); do the math yourself for fixed physical conversions.
The formulas in this post don't change. 1 inch = 2.54 cm is defined, not measured — it will be correct in 50 years.