parseJapaneseNumber関数
リファレンスの説明と例は英語です。見出しと目次は日本語です。
The value of a number written in Japanese, or null when it is not one.
Mixed spellings are the common case rather than an edge: a headline writes 1億2000万 with digits for the awkward parts and characters for the magnitudes, and a textbook writes the same number 一億二千万. Both are read here, and so is a plain run of digits, because the answer wants to go both ways from whichever one was typed.
The formal numerals are read too (壱万弐千, 萬), and so is a thousands comma. Units must descend: 万億 is not a number anybody wrote on purpose, and reading it as one would put a confident wrong answer on the page.
インポート
import { parseJapaneseNumber } from "@johnmorrisdotca/hikidashi/numerals";
ほかのエクスポート元: @johnmorrisdotca/hikidashi
定義
function parseJapaneseNumber(text: string): number | null
引数
| 名前 | 型 | 説明 |
|---|---|---|
text | string | A number in kanji (一億二千万), in digits (120000000, half-width or full-width, with a half-width thousands comma or none), or a mixture (1億2000万), formal numerals included (壱万). |
戻り値
number | null
The value; null for anything that is not a whole number written that way, including a minus sign, a decimal point, kanji digits side by side (一二三), units out of order (万億), a bare 万 and a number past 9,007,199,254,740,991. Null means "not a number I can read", never zero.
例
import { parseJapaneseNumber } from "@johnmorrisdotca/hikidashi/numerals";
console.log(parseJapaneseNumber("一億二千万"), parseJapaneseNumber("1億2000万"), parseJapaneseNumber("2万5千"));
console.log(parseJapaneseNumber("壱万弐千"), parseJapaneseNumber("12000"), parseJapaneseNumber("12,000"));
console.log(parseJapaneseNumber("一二三"), parseJapaneseNumber("万"), parseJapaneseNumber("-5"));120000000 120000000 25000
12000 12000 12000
null null null出力(このページを作ったときに実行した結果)
この日本語は、まだ日本語を母語とする方の確認を受けていません。訂正を歓迎します。