web
9 小时以前 49fa0d82a40345342966e810b44429aec0480ef3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// @ts-nocheck
import { isString } from "../isString";
import { isNumber } from "../isNumber";
/**
 * 将金额转换为中文大写形式
 * @param {number | string} amount - 需要转换的金额,可以是数字或字符串
 * @returns {string} 转换后的中文大写金额
 */
function capitalizedAmount(amount : number) : string
function capitalizedAmount(amount : string) : string
function capitalizedAmount(amount : any | null) : string {
    try {
        let _amountStr :string;
        let _amountNum :number = 0;
        // 如果输入是字符串,先将其转换为数字,并去除逗号
        if (typeof amount == 'string') {
             _amountNum = parseFloat((amount as string).replace(/,/g, ''));
        }
        if(isNumber(amount)) {
            _amountNum = amount as number
        }
        // 判断输入是否为有效的金额  || isNaN(amount)
        if (amount == null) throw new Error('不是有效的金额!');
 
        let result = '';
 
        // 处理负数情况
        if (_amountNum < 0) {
            result = '欠';
            _amountNum = Math.abs(_amountNum);
        }
 
        // 金额不能超过千亿以上
        if (_amountNum >= 10e11) throw new Error('计算金额过大!');
 
        // 保留两位小数并转换为字符串
        _amountStr = _amountNum.toFixed(2);
 
        // 定义数字、单位和小数单位的映射
        const digits = ['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖'];
        const units = ['', '拾', '佰', '仟'];
        const bigUnits = ['', '万', '亿'];
        const decimalUnits = ['角', '分'];
 
        // 分离整数部分和小数部分
        const amountArray = _amountStr.split('.');
        let integerPart = amountArray[0]; // string| number[]
        const decimalPart = amountArray[1];
 
        // 处理整数部分
        if (integerPart != '0') {
            let _integerPart = integerPart.split('').map((item):number => parseInt(item));
 
            // 将整数部分按四位一级进行分组
            const levels = _integerPart.reverse().reduce((prev:string[][], item, index):string[][] => {
                // const level = prev?.[0]?.length < 4 ? prev[0] : [];
                const level = prev.length > 0 && prev[0].length < 4 ? prev[0]: []
 
                const value = item == 0 ? digits[item] : digits[item] + units[index % 4];
 
                level.unshift(value);
 
                if (level.length == 1) {
                    prev.unshift(level);
                } else {
                    prev[0] = level;
                }
 
                return prev;
            }, [] as string[][]);
            // 将分组后的整数部分转换为中文大写形式
            result += levels.reduce((prev, item, index):string => {
                let _level = bigUnits[levels.length - index - 1];
                let _item = item.join('').replace(/(零)\1+/g, '$1');
 
                if (_item == '零') {
                    _level = '';
                    _item = '';
                } else if (_item.endsWith('零')) {
                    _item = _item.slice(0, _item.length - 1);
                }
 
                return prev + _item + _level;
            }, '');
        } else {
            result += '零';
        }
 
        // 添加元
        result += '元';
 
        // 处理小数部分
        if (decimalPart != '00') {
            if (result == '零元') result = '';
 
            for (let i = 0; i < decimalPart.length; i++) {
                const digit = parseInt(decimalPart.charAt(i));
 
                if (digit != 0) {
                    result += digits[digit] + decimalUnits[i];
                }
            }
        } else {
            result += '整';
        }
 
        return result;
    } catch (error : Error) {
        return error.message;
    }
};