From 8349d66fca0f49ed6bc45a7b60b1942a9b7208dc Mon Sep 17 00:00:00 2001 From: kuangshen04 <2832899707@qq.com> Date: Sun, 4 Feb 2024 13:48:37 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=9F=90=E4=BA=9B=E6=83=85?= =?UTF-8?q?=E5=86=B5=E4=B8=8Bget.cnNumber=E7=BF=BB=E8=AF=91=E9=94=99?= =?UTF-8?q?=E8=AF=AF=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- noname/get/index.js | 42 ++++++++++++++++++++++++------------------ 1 file changed, 24 insertions(+), 18 deletions(-) diff --git a/noname/get/index.js b/noname/get/index.js index 1d61b6750..fca92edda 100644 --- a/noname/get/index.js +++ b/noname/get/index.js @@ -2231,46 +2231,52 @@ export class Get extends Uninstantable { default: return num.toString(); } } - static cnNumber(num, two) { - if (num == Infinity) return '∞'; - if (typeof num != 'number' && typeof num != 'string') return num; + static cnNumber(num, ordinal) { if (isNaN(num)) return ''; let numStr = num.toString(); + if (num === 'Infinity') return '∞'; + if (num === '-Infinity') return '-∞'; if (!/^\d+$/.test(numStr)) return num; const chars = ['零', '一', '二', '三', '四', '五', '六', '七', '八', '九']; const units = ['', '十', '百', '千']; if (numStr.length <= 2) {//两位数以下单独处理保证效率 - if (numStr.length == 1) return !two && num == 2 ? '两' : chars[num]; - return (numStr[0] == '1' ? '' : chars[numStr[0]]) + '十' + (numStr[1] == '0' ? '' : chars[numStr[1]]); + if (numStr.length === 1) return !ordinal && num === 2 ? '两' : chars[num]; + return `${numStr[0] === '1' ? '' : chars[numStr[0]]}十${numStr[1] === '0' ? '' : chars[numStr[1]]}`; } numStr = numStr.replace(/(?=(\d{4})+$)/g, ',').split(',').filter(Boolean); - const handleZero = str => str.replace(/零{2,}/g, '零').replace(/零+$/g, ''); + const handleZero = str => str.replace(/零{2,}/g, '零').replace(/(?<=.+)零+$/g, ''); const _transform = str => { - if (str === '0000') return '零'; - if (!two && str === '2') return '两'; + if (str === '2' && !ordinal) return '两'; let result = ''; for (let i = 0; i < str.length; i++) { - let char = chars[+str[i]]; - const unitIndex = str.length - 1 - i; - let unit = units[unitIndex]; - if (!two && char === '二' && unitIndex > 1) char = '两'; + const part = str[str.length - 1 - i]; + let char = chars[+part]; + let unit = units[i]; if (char === '零') unit = ''; - result += char + unit; + else if (char === '一' && i === 1) char = ''; + else if (char === '二' && i > 1 && !ordinal) char = '两'; + result = char + unit + result; } result = handleZero(result); return result; }; let result = ''; + let tempYi = ''; for (let i = 0; i < numStr.length; i++) { - const part = numStr[i]; + const part = numStr[numStr.length - 1 - i]; let char = _transform(part); - const unitIndex = numStr.length - 1 - i; - let unit = unitIndex % 2 ? '万' : '亿'.repeat(unitIndex / 2); - if (char === '零') unit = ''; - result += char + unit; + let unit = ''; + if (i % 2) { + [unit, tempYi] = ['万' + tempYi, '']; + if (char === '零') unit = ''; + } else { + unit = '亿'.repeat(i / 2); + if (char === '零') [unit, tempYi] = ['', unit]; + } + result = char + unit + result; } result = handleZero(result); return result;