// 金额转换字体
const numberToChinese = (num) => {
const units = '拾佰仟万亿';
const chars = '零壹贰叁肆伍陆柒捌玖';
const result = [];
let integerPart = Math.floor(num);
let decimalPart = Math.round((num - integerPart) * 100);
for (let i = 0; integerPart > 0; i++) {
const unitIndex = i % 4;
const numIndex = integerPart % 10;
if (numIndex !== 0 || (unitIndex === 0 && result.length > 0)) {
result.unshift(chars[numIndex] + (unitIndex > 0 ? units[unitIndex - 1] : ''));
}
integerPart = Math.floor(integerPart / 10);
}
const integerPartChinese = result.join('') || '零元整';
let decimalPartChinese = '';
if (decimalPart > 0) {
decimalPartChinese = '点';
const decimalStr = decimalPart.toString().padStart(2, '0');
for (const digit of decimalStr) {
decimalPartChinese += chars[Number(digit)];
}
}
return integerPartChinese + decimalPartChinese;
}
时小记,终有成。