DEV Community

EtsyOptima
EtsyOptima

Posted on

Building a Feet-to-Miles Converter That Doesn't Drift: Exact Constants and Floating Point

Feet to miles looks like the easiest converter you could build: divide by 5,280 and you are done. Then someone types 13 feet, swaps the direction, and gets 12.999999999999998 back. Here is what goes wrong, and a few small habits that keep a unit converter honest.

Start from exact definitions

Some unit relationships are measurements; these ones are definitions. Since the 1959 international yard and pound agreement:

  • 1 foot = 0.3048 m exactly
  • 1 mile = 5,280 ft = 1,609.344 m exactly

So the conversion itself has no uncertainty. Any error in your output is introduced by your code. Note which foot you mean, too: the older US survey foot (1200/3937 m) has been deprecated, and mixing the two shifts a mile by about 3 mm. Small, but enough to fail a test.

The floating-point trap

JavaScript numbers are IEEE 754 doubles. Most decimal fractions, including 1/5,280, cannot be represented exactly, so tiny errors appear:

(13 / 5280) * 5280;   // 12.999999999999998
0.1 + 0.2;            // 0.30000000000000004
Enter fullscreen mode Exit fullscreen mode

In a quick test loop, about 15% of whole-number feet values from 0 to 999,999 failed to come back exactly after dividing and multiplying by 5,280. Nobody wants to see that on screen.

Three habits that fix it

1. Keep the user's input as the source of truth. When the user swaps direction, convert from the original value, not from a rounded or recomputed result.

2. Round once, at display time. Store full precision internally and format only for output:

const FT_PER_MILE = 5280;

const ftToMiles = (ft) => ft / FT_PER_MILE;
const milesToFt = (mi) => mi * FT_PER_MILE;

const format = (x, dp = 4) =>
  new Intl.NumberFormat("en-US", { maximumFractionDigits: dp }).format(x);

format(ftToMiles(35000));     // "6.6288"
format(milesToFt(13 / 5280)); // "13"
Enter fullscreen mode Exit fullscreen mode

Rounding a stored value and then converting back is worse: 1,000 ft rounded to 0.1894 mi becomes 1,000.032 ft on the return trip.

3. Let the user choose precision. Two decimals suits a walking route; survey and engineering work may need six or more. A precision setting is cheaper than arguing about the right default.

Parsing input

People type 5,280, 1/4 or 35000. Strip thousands separators, trim whitespace and support simple fractions before calling Number(), and treat NaN as an empty result rather than showing it.

Test against a reference

I keep a few known pairs in my tests: 5,280 ft = 1 mi, 2,640 ft = 0.5 mi, 35,000 ft ≈ 6.6288 mi. When I want an independent check, I compare against Ft to Miles, which uses the exact 5,280-foot constant, offers 0 to 10 decimal places and rounds only on screen. Its write-up on converting feet to miles in Excel and Google Sheets is useful if your data lives in a spreadsheet rather than code.

The broader lesson applies to any converter: encode exact constants, carry full precision, and treat rounding as presentation.

Top comments (0)