修复某些情况下get.cnNumber翻译错误的问题

This commit is contained in:
kuangshen04 2024-02-04 13:48:37 +08:00
parent d9022bb482
commit 8349d66fca
1 changed files with 24 additions and 18 deletions

View File

@ -2231,46 +2231,52 @@ export class Get extends Uninstantable {
default: return num.toString(); default: return num.toString();
} }
} }
static cnNumber(num, two) { static cnNumber(num, ordinal) {
if (num == Infinity) return '∞';
if (typeof num != 'number' && typeof num != 'string') return num;
if (isNaN(num)) return ''; if (isNaN(num)) return '';
let numStr = num.toString(); let numStr = num.toString();
if (num === 'Infinity') return '∞';
if (num === '-Infinity') return '-∞';
if (!/^\d+$/.test(numStr)) return num; if (!/^\d+$/.test(numStr)) return num;
const chars = ['零', '一', '二', '三', '四', '五', '六', '七', '八', '九']; const chars = ['零', '一', '二', '三', '四', '五', '六', '七', '八', '九'];
const units = ['', '十', '百', '千']; const units = ['', '十', '百', '千'];
if (numStr.length <= 2) {//两位数以下单独处理保证效率 if (numStr.length <= 2) {//两位数以下单独处理保证效率
if (numStr.length == 1) return !two && num == 2 ? '两' : chars[num]; if (numStr.length === 1) return !ordinal && num === 2 ? '两' : chars[num];
return (numStr[0] == '1' ? '' : chars[numStr[0]]) + '十' + (numStr[1] == '0' ? '' : chars[numStr[1]]); return `${numStr[0] === '1' ? '' : chars[numStr[0]]}${numStr[1] === '0' ? '' : chars[numStr[1]]}`;
} }
numStr = numStr.replace(/(?=(\d{4})+$)/g, ',').split(',').filter(Boolean); 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 => { const _transform = str => {
if (str === '0000') return '零'; if (str === '2' && !ordinal) return '两';
if (!two && str === '2') return '两';
let result = ''; let result = '';
for (let i = 0; i < str.length; i++) { for (let i = 0; i < str.length; i++) {
let char = chars[+str[i]]; const part = str[str.length - 1 - i];
const unitIndex = str.length - 1 - i; let char = chars[+part];
let unit = units[unitIndex]; let unit = units[i];
if (!two && char === '二' && unitIndex > 1) char = '两';
if (char === '零') unit = ''; 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); result = handleZero(result);
return result; return result;
}; };
let result = ''; let result = '';
let tempYi = '';
for (let i = 0; i < numStr.length; i++) { for (let i = 0; i < numStr.length; i++) {
const part = numStr[i]; const part = numStr[numStr.length - 1 - i];
let char = _transform(part); let char = _transform(part);
const unitIndex = numStr.length - 1 - i; let unit = '';
let unit = unitIndex % 2 ? '万' : '亿'.repeat(unitIndex / 2); if (i % 2) {
if (char === '零') unit = ''; [unit, tempYi] = ['万' + tempYi, ''];
result += char + unit; if (char === '零') unit = '';
} else {
unit = '亿'.repeat(i / 2);
if (char === '零') [unit, tempYi] = ['', unit];
}
result = char + unit + result;
} }
result = handleZero(result); result = handleZero(result);
return result; return result;