本文へ移動

Hikidashi引き出し

和暦と漢数字の相互変換、活用した語の辞書形、漢字への読みの割り当て、貼り付けた文からの単語の抽出、文の難しさ。日本語のテキストを扱う小さな道具を集めた引き出しです。

「引き出し」という名前は、道具が一つずつしまってあり、必要なときに取り出して使える、という意味です。 名前について(英語)

目次

リファレンス · 漢数字

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

引数

名前型説明
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 (壱万).

戻り値

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

出力(このページを作ったときに実行した結果)

ソースを見るGitHub で編集@johnmorrisdotca/hikidashi 1.1.0 のソースから作成しました。

この日本語は、まだ日本語を母語とする方の確認を受けていません。訂正を歓迎します。