Tools•11 min read
Unit Converters: Complete Guide for Developers
Master unit conversion for length, weight, volume, area, and temperature. Learn formulas, implementation, and best practices.
By Andy Pham
Unit Converters: Complete Guide for Developers
Unit conversion is essential in applications dealing with measurements. This guide covers all major unit types with formulas and implementations.
Length Conversion
Common Units & Relationships
| Unit | Meters | Abbreviation |
|---|---|---|
| Kilometer | 1000 | km |
| Meter | 1 | m |
| Centimeter | 0.01 | cm |
| Millimeter | 0.001 | mm |
| Mile | 1609.344 | mi |
| Yard | 0.9144 | yd |
| Foot | 0.3048 | ft |
| Inch | 0.0254 | in |
Implementation
const lengthFactors = {
km: 1000,
m: 1,
cm: 0.01,
mm: 0.001,
mi: 1609.344,
yd: 0.9144,
ft: 0.3048,
in: 0.0254
};
function convertLength(value, from, to) {
const meters = value * lengthFactors[from];
return meters / lengthFactors[to];
}
// Examples
console.log(convertLength(1, 'mi', 'km')); // 1.609344
console.log(convertLength(100, 'cm', 'in')); // 39.3701
Weight/Mass Conversion
Common Units & Relationships
| Unit | Grams | Abbreviation |
|---|---|---|
| Metric ton | 1000000 | t |
| Kilogram | 1000 | kg |
| Gram | 1 | g |
| Milligram | 0.001 | mg |
| Pound | 453.592 | lb |
| Ounce | 28.3495 | oz |
| Stone | 6350.29 | st |
Implementation
const weightFactors = {
t: 1000000,
kg: 1000,
g: 1,
mg: 0.001,
lb: 453.592,
oz: 28.3495,
st: 6350.29
};
function convertWeight(value, from, to) {
const grams = value * weightFactors[from];
return grams / weightFactors[to];
}
// Examples
console.log(convertWeight(1, 'kg', 'lb')); // 2.20462
console.log(convertWeight(16, 'oz', 'lb')); // 1
Temperature Conversion
Formulas
Celsius to Fahrenheit: F = C × 9/5 + 32
Fahrenheit to Celsius: C = (F - 32) × 5/9
Celsius to Kelvin: K = C + 273.15
Kelvin to Celsius: C = K - 273.15
Implementation
const tempConverters = {
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
};
function convertTemp(value, from, to) {
if (from === to) return value;
// Convert to Celsius first, then to target
let celsius;
switch (from) {
case 'C': celsius = value; break;
case 'F': celsius = tempConverters.FtoC(value); break;
case 'K': celsius = tempConverters.KtoC(value); break;
}
switch (to) {
case 'C': return celsius;
case 'F': return tempConverters.CtoF(celsius);
case 'K': return tempConverters.CtoK(celsius);
}
}
// Examples
console.log(convertTemp(100, 'C', 'F')); // 212
console.log(convertTemp(32, 'F', 'C')); // 0
console.log(convertTemp(0, 'C', 'K')); // 273.15
Volume Conversion
Common Units & Relationships
| Unit | Liters | Abbreviation |
|---|---|---|
| Cubic meter | 1000 | m³ |
| Liter | 1 | L |
| Milliliter | 0.001 | mL |
| Gallon (US) | 3.78541 | gal |
| Quart (US) | 0.946353 | qt |
| Pint (US) | 0.473176 | pt |
| Cup (US) | 0.236588 | cup |
| Fluid oz (US) | 0.0295735 | fl oz |
Implementation
const volumeFactors = {
'm3': 1000,
'L': 1,
'mL': 0.001,
'gal': 3.78541,
'qt': 0.946353,
'pt': 0.473176,
'cup': 0.236588,
'floz': 0.0295735
};
function convertVolume(value, from, to) {
const liters = value * volumeFactors[from];
return liters / volumeFactors[to];
}
// Examples
console.log(convertVolume(1, 'gal', 'L')); // 3.78541
console.log(convertVolume(1000, 'mL', 'L')); // 1
Area Conversion
Common Units & Relationships
| Unit | Square Meters | Abbreviation |
|---|---|---|
| Square kilometer | 1000000 | km² |
| Hectare | 10000 | ha |
| Square meter | 1 | m² |
| Square centimeter | 0.0001 | cm² |
| Square mile | 2589988.11 | mi² |
| Acre | 4046.86 | ac |
| Square yard | 0.836127 | yd² |
| Square foot | 0.092903 | ft² |
| Square inch | 0.00064516 | in² |
Implementation
const areaFactors = {
'km2': 1000000,
'ha': 10000,
'm2': 1,
'cm2': 0.0001,
'mi2': 2589988.11,
'ac': 4046.86,
'yd2': 0.836127,
'ft2': 0.092903,
'in2': 0.00064516
};
function convertArea(value, from, to) {
const sqMeters = value * areaFactors[from];
return sqMeters / areaFactors[to];
}
// Examples
console.log(convertArea(1, 'ac', 'm2')); // 4046.86
console.log(convertArea(1, 'km2', 'mi2')); // 0.386102
Universal Converter Class
class UnitConverter {
constructor() {
this.conversions = {
length: { /* factors */ },
weight: { /* factors */ },
volume: { /* factors */ },
area: { /* factors */ }
};
}
convert(value, from, to, type) {
const factors = this.conversions[type];
if (!factors) throw new Error('Unknown unit type');
const baseValue = value * factors[from];
return baseValue / factors[to];
}
// Format with appropriate precision
formatResult(value, precision = 6) {
return parseFloat(value.toPrecision(precision));
}
}
Best Practices
- Use standard base units for internal calculations
- Round appropriately based on precision needed
- Handle edge cases (negative values, zero, etc.)
- Consider significant figures in results
function smartRound(value, sigFigs = 6) {
if (value === 0) return 0;
const magnitude = Math.floor(Math.log10(Math.abs(value)));
const precision = Math.pow(10, magnitude - sigFigs + 1);
return Math.round(value / precision) * precision;
}
Try Our Converters
- Length Converter - Convert distances
- Weight Converter - Convert mass
- Temperature Converter - Convert temperatures
- Volume Converter - Convert volumes
- Area Converter - Convert areas
Conclusion
Unit conversion requires understanding of base units and conversion factors. Use established formulas, handle precision carefully, and always validate inputs for robust applications.