財務系統、合同打印、支付憑證要求金額以大寫中文數字(壹貳參...)書寫,防止竄改。本文提供一個健壯、處理邊界情況完善的轉換函數。
/**
* 數字轉大寫人民幣
* @param {number|string} money - 金額,支持整數、小數、字串
* @returns {string} 大寫金額,如 "壹佰貳拾叁元整"
*/
function numberToChineseCurrency(money) {
// 1. 常量定義
const CN_NUM = ['零', '壹', '貳', '參', '肆', '伍', '陸', '柒', '捌', '玖'];
const CN_UNIT = ['', '拾', '佰', '仟']; // 節內單位
const CN_SECTION = ['', '萬', '億', '兆']; // 節權位
const CN_DECIMAL = ['角', '分', '釐', '毫']; // 小數單位
const CN_INTEGER = '整';
const CN_YUAN = '元';
const MAX_NUMBER = 999999999999999.9999; // 處理上限
// 2. 邊界處理
if (money === '' || money == null) return '';
money = Number(money);
if (isNaN(money)) return '';
if (money >= MAX_NUMBER) throw new Error('超出最大處理數字 ' + MAX_NUMBER);
if (money === 0) return CN_NUM[0] + CN_YUAN + CN_INTEGER;
// 3. 分離整數/小數部分(字串處理避免浮點誤差)
const str = money.toString();
let [intPart, decPart = ''] = str.split('.');
decPart = decPart.slice(0, 4); // 保留 4 位小數(角分釐毫)
let result = '';
// 4. 整數部分處理(從低位向高位,每 4 位一節)
if (parseInt(intPart, 10) > 0) {
let zeroCount = 0;
for (let i = 0; i < intPart.length; i++) {
const digit = parseInt(intPart[i], 10);
const pos = intPart.length - i - 1; // 從個位起算位置
const section = Math.floor(pos / 4); // 萬/億/兆節索引
const unitPos = pos % 4; // 節內位置:0個 1拾 2佰 3仟
if (digit === 0) {
zeroCount++;
} else {
if (zeroCount > 0) result += CN_NUM[0]; // 補零
zeroCount = 0;
result += CN_NUM[digit] + CN_UNIT[unitPos];
}
// 節權位:每 4 位結束且非全零時添加 萬/億/兆
if (unitPos === 0 && zeroCount < 4) {
result += CN_SECTION[section];
}
}
result += CN_YUAN;
}
// 5. 小數部分處理
if (decPart) {
for (let i = 0; i < decPart.length; i++) {
const digit = parseInt(decPart[i], 10);
if (digit !== 0) {
result += CN_NUM[digit] + CN_DECIMAL[i];
}
}
}
// 6. 收尾
if (!result) result = CN_NUM[0] + CN_YUAN + CN_INTEGER;
else if (!decPart) result += CN_INTEGER;
return result;
}
| 輸入 | 輸出 | 說明 |
|---|---|---|
123 | 壹佰貳拾參元整 | 整數 |
123.45 | 壹佰貳拾參元肆角伍分 | 兩位小數 |
1001 | 壹仟零壹元整 | 中間連續零 |
10000 | 壹萬元整 | 萬位 |
100000000 | 壹億元整 | 億位 |
0.01 | 壹分 | 純小數 |
0 | 零元整 | 零值 |
1234567890.12 | 壹拾貳億叁仟肆佰伍拾陸萬柒仟捌佰玖拾元壹角貳分 | 大數字 |
money.toString().split('.') 而非數學取模。pos / 4 定節,pos % 4 定節內單位。const toChineseCurrency = (money) => {
const CN = ['零','壹','貳','參','肆','伍','陸','柒','捌','玖'];
const UNIT = ['','拾','佰','仟'];
const SEC = ['','萬','億','兆'];
const DEC = ['角','分','釐','毫'];
if (!money && money !== 0) return '';
const n = Number(money);
if (n === 0) return CN[0] + '元整';
const [intS, decS = ''] = n.toString().split('.');
let res = '';
// 整數部分
for (let i = 0; i < intS.length; i++) {
const d = +intS[i];
const p = intS.length - i - 1;
if (d) {
if (p % 4 === 0 && i !== 0) res += SEC[p/4];
res += CN[d] + UNIT[p%4];
} else if (p % 4 === 0 && i !== 0 && intS.slice(i-3,i).some(c=>c!=='0')) {
res += CN[0] + SEC[p/4]; // 補零+節權位
} else if (res && res.slice(-1) !== CN[0]) {
res += CN[0]; // 補單零
}
}
res += '元';
// 小數部分
for (let i = 0; i < decS.length && i < 4; i++) {
if (+decS[i]) res += CN[+decS[i]] + DEC[i];
}
return res + (decS ? '' : '整');
};
大寫金額轉換核心是「四位一節、零處理、單位映射」。提供的函數已處理:整數/小數、零值、大數字、中間連續零、節權位(萬/億/兆)、小數四位(角分釐毫)、浮點精度。生產環境建議加入單元測試覆蓋邊界案例,並根據業務決定是否支援負數、科學記數法等擴展。