完成仓库模块
This commit is contained in:
82
src/utils/money.ts
Normal file
82
src/utils/money.ts
Normal file
@@ -0,0 +1,82 @@
|
||||
/**
|
||||
* 数字金额转中文大写(支持整数、小数)
|
||||
* @param num - 待转换的金额(支持数字或字符串格式)
|
||||
* @returns 中文大写金额字符串
|
||||
*/
|
||||
export function convertToChineseCapital(num: number | string): string {
|
||||
// 中文数字映射
|
||||
const digits: readonly string[] = ['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖'];
|
||||
// 基本单位(个、拾、佰、仟)
|
||||
const units: readonly string[] = ['', '拾', '佰', '仟'];
|
||||
// 大单位(万、亿、万亿)
|
||||
const bigUnits: readonly string[] = ['', '万', '亿', '万亿'];
|
||||
|
||||
// 1. 标准化输入:保留4位小数,分割整数与小数部分
|
||||
const [intPartRaw, decPartRaw] = Number(num).toFixed(4).split('.');
|
||||
// 去除整数前导零,若全零则保留一个 '0'
|
||||
const intPart: string = intPartRaw.replace(/^0+/, '') || '0';
|
||||
|
||||
let result: string = '';
|
||||
|
||||
// 2. 处理整数部分
|
||||
if (intPart === '0') {
|
||||
result = '零元';
|
||||
} else {
|
||||
// 从右向左,每4位分为一组(处理万、亿等大单位)
|
||||
const groups: string[] = [];
|
||||
let tempInt: string = intPart;
|
||||
while (tempInt.length > 0) {
|
||||
groups.unshift(tempInt.slice(-4));
|
||||
tempInt = tempInt.slice(0, -4);
|
||||
}
|
||||
|
||||
// 遍历每一组进行转换
|
||||
groups.forEach((group: string, gIndex: number) => {
|
||||
let groupStr: string = '';
|
||||
let needZero: boolean = false; // 标记是否需要补零
|
||||
|
||||
for (let i = 0; i < group.length; i++) {
|
||||
const digit: number = parseInt(group[i], 10);
|
||||
const unitPos: number = group.length - 1 - i; // 当前数字对应的单位位置
|
||||
|
||||
if (digit === 0) {
|
||||
needZero = true;
|
||||
} else {
|
||||
// 补零(如果前面有0)
|
||||
if (needZero) {
|
||||
groupStr += '零';
|
||||
needZero = false;
|
||||
}
|
||||
groupStr += digits[digit] + units[unitPos];
|
||||
}
|
||||
}
|
||||
|
||||
// 拼接大单位(万、亿)
|
||||
if (groupStr) {
|
||||
result += groupStr + bigUnits[groups.length - 1 - gIndex];
|
||||
}
|
||||
});
|
||||
result += '元';
|
||||
}
|
||||
|
||||
// 3. 处理小数部分(仅保留角、分,忽略毫、厘)
|
||||
const jiao: number = parseInt(decPartRaw[0], 10); // 角(第1位小数)
|
||||
const fen: number = parseInt(decPartRaw[1], 10); // 分(第2位小数)
|
||||
|
||||
if (jiao === 0 && fen === 0) {
|
||||
result += '整';
|
||||
} else {
|
||||
// 处理“角”
|
||||
if (jiao > 0) {
|
||||
result += digits[jiao] + '角';
|
||||
} else if (jiao === 0 && fen > 0) {
|
||||
result += '零';
|
||||
}
|
||||
// 处理“分”
|
||||
if (fen > 0) {
|
||||
result += digits[fen] + '分';
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
Reference in New Issue
Block a user