parseEnglishNumberfunction
The value of a number written in words, or null when it is not one.
Digits are allowed inside it - "5 man", "20 thousand" - since that is how somebody actually types a magnitude they know the name of. Anything the table does not hold makes the whole thing null rather than a partial reading: "five cats" is not five, and answering as though it were would be a confident wrong answer to a question nobody asked.
Import it
import { parseEnglishNumber } from "@johnmorrisdotca/hikidashi/numerals";
Also exported from: @johnmorrisdotca/hikidashi
Definition
function parseEnglishNumber(text: string): number | null
Parameters
| Name | Type | Description |
|---|---|---|
text | string | A number in English words ("five hundred", "twenty-five", "1.2 million"), or with a Japanese magnitude in romaji ("5 man", "3 oku"); digits may stand for any part. |
Returns
number | null
The value; null when any word is not a number word ("five cats"), when there is nothing to read, or when the value is past 9,007,199,254,740,991 either way.
Examples
import { parseEnglishNumber } from "@johnmorrisdotca/hikidashi/numerals";
console.log(parseEnglishNumber("five hundred"), parseEnglishNumber("twenty-five thousand"), parseEnglishNumber("5 man"));
console.log(parseEnglishNumber("1.2 million"), parseEnglishNumber("five cats"));500 25000 50000
1200000 nullWhat it prints (run when this page was built)