Skip to content
Back to Blog
Tutorials

Unit Conversion Guide: Length, Weight, Temperature & Volume Formulas

Master metric-to-imperial conversion with exact formulas, code examples & quick-reference tables for length, weight, temperature and volume. Free tools included.

14 min read

The Complete Unit Conversion Guide: Formulas, Code & Tools for Every Measurement

A recipe calls for 350°F and you only know Celsius. A shipping label reads 22 lbs and your system expects kilograms. A client sends dimensions in feet and your CAD software uses meters. Unit conversion is one of those tasks that sounds trivial until you get it wrong — and the consequences range from a ruined dinner to a $327 million spacecraft crash.

This guide covers the four measurement types you will convert most often — length, weight, temperature, and volume — with exact formulas, JavaScript and Python code you can copy into your projects, and quick-reference tables for mental math.

Why Unit Conversion Still Trips Up Developers

You might think unit errors are a solved problem. They are not.

In 1999, NASA’s Mars Climate Orbiter disintegrated in the Martian atmosphere because one engineering team used pound-force seconds while another used newton-seconds. Nobody caught the mismatch during months of trajectory calculations. The loss: $327.6 million.

In 1983, Air Canada Flight 143 (the “Gimli Glider”) ran out of fuel mid-flight because ground crew calculated fuel in pounds instead of kilograms. The plane had less than half the fuel it needed.

These are extreme cases, but smaller unit errors happen constantly:

  • US gallon ≠ Imperial gallon. They differ by about 20%. A fuel economy comparison between American and British figures is meaningless without knowing which gallon.
  • Troy ounce ≠ regular ounce. Gold priced at “$1,900 per ounce” uses troy ounces (31.1 g), not the avoirdupois ounce (28.35 g) on your kitchen scale.
  • Temperature conversion is not linear. You cannot just multiply — there is an offset, and forgetting it gives wildly wrong results.

Metric vs Imperial: Two Systems, One Planet

Before diving into formulas, here is the big picture.

DimensionMetric (SI)Imperial / US Customary
Design principleDecimal — every unit scales by powers of 10Inconsistent — 12 in/ft, 3 ft/yd, 5,280 ft/mi
Lengthmeter (m)foot (ft), inch (in), mile (mi)
Mass / Weightkilogram (kg)pound (lb), ounce (oz)
TemperatureCelsius (°C), Kelvin (K)Fahrenheit (°F)
Volumeliter (L)gallon (gal), pint (pt), cup
Used by~95% of the worldUnited States, Myanmar, Liberia

The metric system is maintained by the International Bureau of Weights and Measures (BIPM). Since 2019, all seven SI base units are defined by fixed physical constants — the speed of light, the Planck constant, the Boltzmann constant, and others. This means a meter is the same everywhere in the universe, by definition.

The imperial system actually splits into two variants: US customary (used in America) and Imperial (historically used in Britain). They share names for many units but define some differently — gallons and fluid ounces, for instance, are not the same size.

Length Conversion: Formulas & Code

Length is the most frequently converted measurement type. Use our length converter for instant results, or use the formulas below in your own code.

Key Length Conversion Factors

All of these are exact values, established by the 1959 international yard and pound agreement:

ConversionFactorDirection
inches → centimeters× 2.54exact
feet → meters× 0.3048exact
yards → meters× 0.9144exact
miles → kilometers× 1.609344exact
nautical miles → meters× 1,852exact

Metric prefixes make the rest easy: 1 km = 1,000 m, 1 cm = 0.01 m, 1 mm = 0.001 m. Moving between metric units is just shifting the decimal point.

Length Conversion in Code

JavaScript:

const lengthConvert = {
  inToCm:  (inches) => inches * 2.54,
  cmToIn:  (cm)     => cm / 2.54,
  ftToM:   (feet)   => feet * 0.3048,
  mToFt:   (m)      => m / 0.3048,
  miToKm:  (miles)  => miles * 1.609344,
  kmToMi:  (km)     => km / 1.609344,
};

console.log(lengthConvert.inToCm(27));   // 68.58 (27" monitor)
console.log(lengthConvert.miToKm(26.2)); // 42.164928 (marathon)
console.log(lengthConvert.mToFt(1.83));  // 6.003937... (6 ft person)

Python:

def inches_to_cm(inches: float) -> float:
    return inches * 2.54

def feet_to_meters(feet: float) -> float:
    return feet * 0.3048

def miles_to_km(miles: float) -> float:
    return miles * 1.609344

print(inches_to_cm(27))    # 68.58
print(miles_to_km(26.2))   # 42.164928

Mental Math Tricks for Length

  • Inches to cm: multiply by 2.5. You will be off by 1.6%, close enough for everyday use.
  • Miles to km: use the Fibonacci sequence. Consecutive Fibonacci numbers approximate the conversion ratio: 5 mi ≈ 8 km, 8 mi ≈ 13 km, 13 mi ≈ 21 km.
  • Feet to meters: divide by 3.3. A 6-foot person is about 1.82 m.

Quick Reference: Length

ImperialMetric
1 inch2.54 cm
1 foot30.48 cm
6 feet1.8288 m
1 yard0.9144 m
1 mile1.609 km
1 nautical mile1.852 km

Weight & Mass Conversion: Formulas & Code

Technically, “weight” is the force of gravity on an object while “mass” is the amount of matter. In everyday use and in this guide, we treat them interchangeably. Convert values instantly with our weight converter.

Key Weight Conversion Factors

ConversionFactor
kilograms → pounds× 2.20462
pounds → kilograms× 0.453592
ounces (avoirdupois) → grams× 28.3495
troy ounces → grams× 31.1035
stone → kilograms× 6.35029
stone → pounds× 14 (exact)

The Troy Ounce Trap

If you work with precious metals, gemstones, or financial data, watch out: the troy ounce (31.1035 g) is about 10% heavier than the standard avoirdupois ounce (28.3495 g). Gold, silver, and platinum are always priced per troy ounce. Mixing these up in a financial application means your calculations will be off by 10% — a costly mistake.

Weight Conversion in Code

JavaScript:

const weightConvert = {
  kgToLbs:    (kg)  => kg * 2.20462,
  lbsToKg:    (lbs) => lbs * 0.453592,
  ozToGrams:  (oz)  => oz * 28.3495,
  gramsToOz:  (g)   => g / 28.3495,
  stoneToKg:  (st)  => st * 6.35029,
};

console.log(weightConvert.kgToLbs(70));    // 154.3234 (avg adult)
console.log(weightConvert.ozToGrams(8));   // 226.796 (half pound)
console.log(weightConvert.stoneToKg(11));  // 69.85319

Python:

def kg_to_lbs(kg: float) -> float:
    return kg * 2.20462

def lbs_to_kg(lbs: float) -> float:
    return lbs * 0.453592

def oz_to_grams(oz: float) -> float:
    return oz * 28.3495

print(kg_to_lbs(70))    # 154.3234
print(oz_to_grams(8))   # 226.796

Mental Math Trick for Weight

kg to lbs: double the value, then add 10%. For example, 70 kg → 140 + 14 = 154 lbs. The exact answer is 154.32 — close enough.

Quick Reference: Weight

ImperialMetric
1 ounce28.35 g
1 pound453.6 g
1 stone (14 lbs)6.35 kg
1 short ton (2,000 lbs)907.2 kg
1 troy ounce31.10 g

Temperature Conversion: The One That’s Different

Length, weight, and volume conversions all use simple multiplication. Temperature does not. Temperature scales have different zero points, so conversion requires both multiplication and addition. Try it yourself with our temperature converter.

Temperature Formulas Explained

The four temperature scales and their relationships:

Celsius ↔ Fahrenheit:

°F = °C × 9/5 + 32
°C = (°F − 32) × 5/9

Where does 9/5 come from? Water freezes at 0°C (32°F) and boils at 100°C (212°F). The Fahrenheit range is 212 − 32 = 180 degrees. The Celsius range is 100 degrees. 180/100 = 9/5.

Celsius ↔ Kelvin:

K = °C + 273.15
°C = K − 273.15

Kelvin uses the same degree size as Celsius but starts at absolute zero (−273.15°C). There are no negative Kelvin values.

Fahrenheit ↔ Rankine:

°R = °F + 459.67

Rankine is to Fahrenheit what Kelvin is to Celsius — same degree size, but starting from absolute zero. Used mainly in American engineering thermodynamics.

Fun fact: −40° is where Celsius and Fahrenheit intersect. −40°C = −40°F exactly.

Temperature Conversion in Code

JavaScript:

const tempConvert = {
  cToF: (c) => c * 9 / 5 + 32,
  fToC: (f) => (f - 32) * 5 / 9,
  cToK: (c) => c + 273.15,
  kToC: (k) => k - 273.15,
  fToK: (f) => (f - 32) * 5 / 9 + 273.15,
  kToF: (k) => (k - 273.15) * 9 / 5 + 32,
};

console.log(tempConvert.cToF(100));  // 212 (boiling water)
console.log(tempConvert.fToC(98.6)); // 37 (body temperature)
console.log(tempConvert.cToK(0));    // 273.15 (freezing point)

Python:

def c_to_f(c: float) -> float:
    return c * 9 / 5 + 32

def f_to_c(f: float) -> float:
    return (f - 32) * 5 / 9

def c_to_k(c: float) -> float:
    return c + 273.15

print(c_to_f(100))   # 212.0
print(f_to_c(98.6))  # 37.0
print(c_to_k(-273.15))  # 0.0 (absolute zero)

Mental Math Trick for Temperature

For a rough Celsius-to-Fahrenheit estimate: double and add 30. This works well in the 0–30°C range:

  • 20°C → 40 + 30 = 70°F (exact: 68°F)
  • 30°C → 60 + 30 = 90°F (exact: 86°F)

The error grows at extreme temperatures, but for weather and cooking this gets you close enough.

Key Temperature Benchmarks

Event°C°FK
Absolute zero−273.15−459.670
Water freezes032273.15
Room temperature20–2268–72293–295
Human body3798.6310.15
Water boils100212373.15
Oven (moderate)180356453.15

Volume & Liquid Conversion: Formulas & Code

Volume conversion has a unique pitfall: the US gallon and the Imperial gallon are not the same size. Convert between 15 volume units with our volume converter.

Key Volume Conversion Factors

ConversionFactor
liters → US gallons÷ 3.78541
liters → Imperial gallons÷ 4.54609
US cups → milliliters× 236.588
US fluid ounces → milliliters× 29.5735
US tablespoons → milliliters× 14.787
US teaspoons → milliliters× 4.929

The US vs Imperial Gallon Trap

This catches people more often than you would expect:

UnitUS CustomaryImperial (UK)Difference
1 gallon3.785 L4.546 L20.1%
1 fluid ounce29.57 mL28.41 mL3.9%
1 pint473.2 mL568.3 mL20.1%

The US gallon descends from the 1707 English wine gallon. The Imperial gallon was redefined in 1824 as the volume of 10 pounds of water at 62°F. The result: an Imperial gallon is about one-fifth larger.

This matters in real scenarios. If a British car review quotes fuel economy in miles per gallon, those are Imperial gallons. Comparing directly with US mpg figures — without converting — makes the British car look 20% more efficient than it actually is relative to a US measurement.

Volume Conversion in Code

JavaScript:

const volumeConvert = {
  litersToUSGal:  (l)    => l / 3.78541,
  usGalToLiters:  (gal)  => gal * 3.78541,
  cupsToMl:       (cups) => cups * 236.588,
  mlToCups:       (ml)   => ml / 236.588,
  flOzToMl:       (floz) => floz * 29.5735,
  mlToFlOz:       (ml)   => ml / 29.5735,
};

console.log(volumeConvert.litersToUSGal(3.78541)); // 1 (exact)
console.log(volumeConvert.cupsToMl(2));            // 473.176
console.log(volumeConvert.flOzToMl(12));           // 354.882

Python:

def liters_to_us_gal(liters: float) -> float:
    return liters / 3.78541

def cups_to_ml(cups: float) -> float:
    return cups * 236.588

def fl_oz_to_ml(fl_oz: float) -> float:
    return fl_oz * 29.5735

print(liters_to_us_gal(3.78541))  # ~1.0
print(cups_to_ml(2))              # 473.176

Building a Universal Unit Converter in JavaScript

If you need a converter in your own project, here is a compact implementation. The trick: define every unit’s relationship to a base unit, then convert through the base.

const units = {
  // Length (base: meter)
  m:   { base: 'm', factor: 1 },
  km:  { base: 'm', factor: 1000 },
  cm:  { base: 'm', factor: 0.01 },
  mm:  { base: 'm', factor: 0.001 },
  in:  { base: 'm', factor: 0.0254 },
  ft:  { base: 'm', factor: 0.3048 },
  yd:  { base: 'm', factor: 0.9144 },
  mi:  { base: 'm', factor: 1609.344 },

  // Weight (base: gram)
  g:   { base: 'g', factor: 1 },
  kg:  { base: 'g', factor: 1000 },
  mg:  { base: 'g', factor: 0.001 },
  lb:  { base: 'g', factor: 453.592 },
  oz:  { base: 'g', factor: 28.3495 },

  // Volume (base: milliliter)
  ml:     { base: 'ml', factor: 1 },
  l:      { base: 'ml', factor: 1000 },
  usgal:  { base: 'ml', factor: 3785.41 },
  uscup:  { base: 'ml', factor: 236.588 },
  floz:   { base: 'ml', factor: 29.5735 },
};

function convert(value, fromUnit, toUnit) {
  const from = units[fromUnit];
  const to = units[toUnit];
  if (!from || !to || from.base !== to.base) {
    throw new Error(`Cannot convert ${fromUnit} to ${toUnit}`);
  }
  return value * from.factor / to.factor;
}

// Temperature needs special handling (offset-based)
function convertTemp(value, from, to) {
  // Normalize to Celsius first
  let c;
  if (from === 'C') c = value;
  else if (from === 'F') c = (value - 32) * 5 / 9;
  else if (from === 'K') c = value - 273.15;
  else throw new Error(`Unknown unit: ${from}`);

  // Convert from Celsius to target
  if (to === 'C') return c;
  if (to === 'F') return c * 9 / 5 + 32;
  if (to === 'K') return c + 273.15;
  throw new Error(`Unknown unit: ${to}`);
}

console.log(convert(26.2, 'mi', 'km'));       // 42.164928
console.log(convert(70, 'kg', 'lb'));         // 154.324...
console.log(convert(2, 'uscup', 'ml'));       // 473.176
console.log(convertTemp(100, 'C', 'F'));      // 212

The convert function handles any unit whose relationship to the base is a simple ratio. Temperature gets its own function because of the offset. You can extend units with any new unit — just specify its base type and conversion factor.

FAQ

How many centimeters are in an inch?

Exactly 2.54 cm. This is not an approximation — it is the exact conversion factor established by the 1959 international yard and pound agreement. To convert inches to centimeters, multiply by 2.54. To go the other way, divide by 2.54. Convert lengths instantly with our free tool.

What is the formula to convert Celsius to Fahrenheit?

°F = °C × 9/5 + 32. For example, 20°C = 20 × 1.8 + 32 = 68°F. The 9/5 ratio comes from the 180-degree Fahrenheit span (32°F to 212°F) divided by the 100-degree Celsius span (0°C to 100°C). Check the exact result with our temperature tool.

Why are US and Imperial gallons different?

The US gallon (3.785 L) descends from the 1707 English wine gallon, while the Imperial gallon (4.546 L) was redefined in 1824 based on the volume of 10 pounds of distilled water. This roughly 20% difference is significant for fuel economy comparisons, recipe scaling, and international shipping.

How many pounds are in a kilogram?

1 kg = 2.20462 lbs. For mental math: double the kg value and add 10%. So 70 kg → 140 + 14 = 154 lbs. The precise answer is 154.32 lbs.

What is absolute zero?

0 K = −273.15°C = −459.67°F. It is the theoretical lowest temperature, where all thermal molecular motion ceases. No laboratory has ever achieved exactly 0 K, though experiments have come within billionths of a degree.

Is a troy ounce the same as a regular ounce?

No. A troy ounce (31.1035 g) is about 10% heavier than a standard avoirdupois ounce (28.3495 g). Troy ounces are used exclusively for precious metals — gold, silver, platinum. If you see a gold price quoted “per ounce,” it always means troy ounces.

What is the easiest way to convert miles to kilometers in my head?

Use the Fibonacci sequence. Consecutive Fibonacci numbers approximate the 1:1.609 ratio between miles and kilometers: 5 mi ≈ 8 km, 8 mi ≈ 13 km, 13 mi ≈ 21 km. For arbitrary values, just multiply miles by 1.6.

Wrap-Up

Four things to remember:

  1. Metric is decimal. Moving between metric units means shifting a decimal point. No memorization needed.
  2. Imperial requires lookup. 12 inches in a foot, 3 feet in a yard, 5,280 feet in a mile — there is no pattern.
  3. Temperature has an offset. Unlike other conversions, you cannot just multiply. Always apply the formula: °F = °C × 9/5 + 32.
  4. US ≠ Imperial for liquids. A US gallon and an Imperial gallon differ by 20%. Always check which one you are working with.

Need to convert something right now? Use our free browser-based tools — no signup, no data leaves your device:

Related Articles

View all articles