How a measurement parser reads 12' 6", 2.4 m and 2,5 without annoying users
Online calculators usually force users to convert their measurements into a single number and unit before typing, which frustrates people who measure in feet and inches. The author, who builds the free Toggle9 calculators, describes a dependency-free parser of under 200 lines of JavaScript that accepts natural input such as mixed feet and inches, fractions, vulgar fraction characters, and multiplication expressions. The normaliser standardises varied notations, with rules that treat a comma before exactly three digits as a thousands separator and other digit-comma-digit patterns as decimal commas. A lexer folds fractions into numbers and tags unit tokens with their dimension, so areas and volumes are distinguished from lengths. An evaluator converts every value to SI base units, and the parser returns clear, actionable messages when an input cannot be read or has the wrong dimension for a field.
This is an AI-generated summary. ShortSingh links to the original source for the complete article.

Discussion (0)
Log in to join the discussion and vote.
Log in