const DIGITS: Record = { 零: 0, 〇: 0, 一: 1, 二: 2, 两: 2, 兩: 2, 三: 3, 四: 4, 五: 5, 六: 6, 七: 7, 八: 8, 九: 9, }; const UNITS: Record = { 十: 10, 拾: 10, 百: 100, 千: 1000 }; /** 中文数字/阿拉伯数字 → 整数;无法解析返回 undefined */ export function cnToInt(s: string): number | undefined { const t = s.trim(); if (/^\d+$/.test(t)) return Number.parseInt(t, 10); if (t === "" || ![...t].every((c) => c in DIGITS || c in UNITS)) return undefined; let total = 0; let current = 0; for (const c of t) { const d = DIGITS[c]; if (d !== undefined) { current = d; continue; } const u = UNITS[c]!; total += (current || 1) * u; current = 0; } return total + current; } export const CN_NUM = "[0-9零〇一二两兩三四五六七八九十拾百千]+";