Skip to the content

Hikidashi引き出し

Era dates (和暦) and kanji numerals both ways, the dictionary forms of a conjugated word, a reading shared out over its kanji, words pulled from pasted text, and how hard a sentence is to read.

Hikidashi (引き出し) is a drawer: each tool is pulled out when it is wanted. About the name

Contents

Reference · Kanji numerals

parseJapaneseNumberfunction

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 it

import { parseJapaneseNumber } from "@johnmorrisdotca/hikidashi/numerals";

Also exported from: @johnmorrisdotca/hikidashi

Definition

function parseJapaneseNumber(text: string): number | null

Parameters

NameTypeDescription
textstring

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 (壱万).

Returns

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.

Examples

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

What it prints (run when this page was built)

View sourceEdit on GitHubMade from the source of @johnmorrisdotca/hikidashi 1.1.0.