Words to Number
Parse English number words back to digits.
Overview
Parse English number words back into their numeric values. "Forty-two" becomes 42; "one thousand two hundred thirty-four" becomes 1234; "a million" becomes 1,000,000. The parser is forgiving about hyphens, "and", and conversational phrasing.
Document processors converting written amounts to numeric for further calculation, accessibility tools normalizing speech-to-text output, legal systems checking that a cheque's written amount matches its numeric amount, and developers building voice-friendly UIs all use a words-to-number parser. It's also handy for cleaning up casually-typed survey responses.
How it works
The parser tokenizes the input and walks it left-to-right, accumulating value. Small-number words ("one" through "nineteen") provide direct values; tens-words ("twenty", "thirty"…) multiply the next digit; scale words ("hundred", "thousand", "million", "billion") trigger multiplication of the running subtotal and reset the accumulator for the next group. The word "and" is treated as optional and harmless.
The algorithm handles compound numbers ("forty-two") with or without the hyphen, and gracefully recovers when the input mixes digits and words ("3 hundred forty 2" → 342).
Examples
Input: forty-two
Output: 42
Input: one thousand two hundred thirty-four
Output: 1234
Input: twelve million three hundred forty-five thousand six hundred seventy-eight
Output: 12,345,678
Input: negative seven
Output: -7
FAQ
Does it handle "and"?
Yes. British convention places "and" between hundreds and the remainder ("one hundred and twenty-three"). The parser accepts but doesn't require it.
What about fractions like "one and a half"?
Basic support: "a half" parses to 0.5, "a quarter" to 0.25. Complex fractional phrasing may need a manual touch-up.
Currency phrasing — "five dollars and twenty cents"?
The parser extracts the integer and decimal halves separately when it detects currency phrasing. The result is 5.20.