Skip to content

Unit Converter

Unit Converter recognizes a conversion written as the lookup text and calculates it locally. It is useful for measurements encountered while reading, without depending on a remote service.

Unit Converter showing the result of 10 kilometers converted to miles

Setup

  1. Add or open a Script source in Definer Options.
  2. Replace the code in its editor with the script below.
  3. Look up an expression such as 10 km to mi, 72 f to c, or 5 lb to kg.

Script

js
const query = definer.lookup.query.trim()
const units = 'mm|cm|m|km|in|ft|yd|mi|g|kg|oz|lb|°?[cfk]'
const match = query.match(
	new RegExp(`^(-?(?:\\d+(?:\\.\\d+)?|\\.\\d+))\\s*(${units})\\s+(?:to|in)\\s*(${units})$`, 'i'),
)

definer.output.setHtml(`
	<section class="converter">
		<p class="converter__input"></p>
		<strong class="converter__result"></strong>
		<p class="converter__help">Try “10 km to mi”, “72 f to c”, or “5 lb to kg”.</p>
	</section>
`)
definer.output.setCss(`
	.converter { padding: 20px; }
	.converter__input, .converter__help { margin: 0; opacity: 0.65; }
	.converter__result { display: block; margin: 8px 0; font-size: 1.7em; }
`)

const input = document.querySelector('.converter__input')
const result = document.querySelector('.converter__result')
input.textContent = query

if (!match) {
	result.textContent = 'No supported conversion found.'
} else {
	const value = Number(match[1])
	const from = match[2].toLowerCase().replace('°', '')
	const to = match[3].toLowerCase().replace('°', '')
	const groups = [
		{ mm: 0.001, cm: 0.01, m: 1, km: 1000, in: 0.0254, ft: 0.3048, yd: 0.9144, mi: 1609.344 },
		{ g: 0.001, kg: 1, oz: 0.028349523125, lb: 0.45359237 },
	]
	const group = groups.find((candidate) => from in candidate && to in candidate)
	let converted

	if (group) {
		converted = (value * group[from]) / group[to]
	} else if ('cfk'.includes(from) && 'cfk'.includes(to)) {
		const celsius = from === 'c' ? value : from === 'f' ? (value - 32) * 5 / 9 : value - 273.15
		converted = to === 'c' ? celsius : to === 'f' ? celsius * 9 / 5 + 32 : celsius + 273.15
	}

	result.textContent = Number.isFinite(converted)
		? `${Number(converted.toFixed(6))} ${to === 'c' || to === 'f' ? `°${to.toUpperCase()}` : to}`
		: 'Those units cannot be converted to each other.'
}

Supported units

  • Length: millimeters, centimeters, meters, kilometers, inches, feet, yards, and miles.
  • Mass: grams, kilograms, ounces, and pounds.
  • Temperature: Celsius, Fahrenheit, and Kelvin, with or without a degree symbol.

Use to or in between the source and destination units. Unit names are intentionally short so expressions remain quick to select or type.

How it works

The script parses definer.lookup.query, converts compatible length and mass units through a shared base unit, and handles temperature scales separately. It performs no network request and stores no result data.

Dynamic text is assigned through textContent. Unsupported syntax and incompatible dimensions produce an explanatory result instead of a runtime error.

Customize it

  • Add aliases such as meters or pounds before parsing.
  • Add another linear group for area, volume, or data sizes.
  • Change the rounding policy for scientific or financial use.
  • Keep exchange rates out of the static factor table; currency conversion needs current external data and explicit failure handling.

Return to the Script Catalog.