-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Perf: 完善阿拉伯数字转中文数字函数
- Loading branch information
Showing
4 changed files
with
140 additions
and
107 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
{ | ||
"name": "vue-tpl", | ||
"author": "毛瑞 <[email protected]>", | ||
"version": "1.2.2", | ||
"version": "1.2.3", | ||
"private": false, | ||
"license": "MIT", | ||
"keywords": [ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,104 +1,119 @@ | ||
/** 阿拉伯数字转中文数字(最高支持描述万万亿) | ||
* @param {string|number} number 阿拉伯数字 | ||
* @param {number} unit 1: 不需要单位 2: 不需要单位并且保留开头的0 | ||
* @param {number} describe 描述 默认:使用描述 1:不使用描述 2:不使用描述且保留开头的0 | ||
* @param {number} cases 大小写 默认:中文小写 1:中文小写但是使用大写零 2:中文大写 | ||
* | ||
* @returns {string} 中文数字 | ||
*/ | ||
export default function numToCN(number: string | number, noUnit?: 1 | 2) { | ||
export default function( | ||
number: string | number, | ||
describe?: 0 | 1 | 2, | ||
cases?: 0 | 1 | 2 | ||
) { | ||
if (!number && number !== 0) { | ||
return '' | ||
} | ||
|
||
let NUM: string // 数字 | ||
let UNIT: string // 单位 | ||
let ZERO: string | ||
let TRIM_BEFORE: RegExp // 去开头的0和单位 | ||
if (cases === 2) { | ||
ZERO = '零' | ||
NUM = '零壹贰叁肆伍陆柒捌玖' | ||
UNIT = '拾佰仟万拾佰仟亿拾佰仟万拾佰仟' | ||
TRIM_BEFORE = /^零*[拾佰仟万亿]*零*/ | ||
} else { | ||
ZERO = cases ? '零' : '〇' | ||
NUM = ZERO + '一二三四五六七八九' | ||
UNIT = '十百千万十百千亿十百千万十百千' | ||
TRIM_BEFORE = new RegExp(`^${ZERO}*[十百千万亿]*${ZERO}*`) | ||
cases = 0 // 标识小写 | ||
} | ||
const GOLD = '亿' // 最大单位 | ||
const SILVER = '万' // 溢出单位 一二万万亿 | ||
const POINT = '点' | ||
const NUM = '零一二三四五六七八九' | ||
const UNIT = '十百千万十百千亿十百千万十百千' | ||
const SIGN = { '+': '正', '-': '负' } | ||
const LEN_UNIT = 15 // UNIT.length | ||
const ZERO = '零' // NUM[0] | ||
const UNIT_MAX = '亿' // 最大单位 | ||
const TRIPLE = '万' // 溢出单位 一二万万亿 | ||
const TRIPLE_POS = 11 // 万的最后位置 十万亿 | ||
const TRIM_BEFORE = /^零*[十百千万亿]*/ // 去开头的0和单位 | ||
const SILVER_POS = 11 // 万的最后位置 十万亿 | ||
|
||
return String(number).replace( | ||
/([+-])?(\d+)(\.\d+)?/g, | ||
(match, sign: '+' | '-', integer: string, decimal: string) => { | ||
let result = '' | ||
(char, sign: '+' | '-', integer: string, unit: string) => { | ||
let cn = '' | ||
|
||
let index | ||
// 小数部分(保留末尾0) | ||
if (decimal) { | ||
index = decimal.length | ||
if (unit) { | ||
index = unit.length | ||
while (--index) { | ||
result = NUM[decimal[index] as any] + result | ||
cn = NUM[unit[index] as any] + cn | ||
} | ||
result = POINT + result | ||
cn = POINT + cn | ||
} | ||
|
||
let char | ||
let unit | ||
let pos | ||
let indexUnit = -1 // [-1, LEN_UNIT) | ||
number = -1 // [-1, LEN_UNIT) | ||
index = integer.length | ||
// 整数部分 | ||
while (index--) { | ||
char = NUM[integer[index] as any] | ||
if (!noUnit) { | ||
if (indexUnit < LEN_UNIT) { | ||
if (!describe) { | ||
if (number < LEN_UNIT) { | ||
// 未超过最大可描述数值 | ||
unit = UNIT[indexUnit++] || '' | ||
unit = UNIT[number++] || '' | ||
if (ZERO === char) { | ||
char = result[0] | ||
switch (char) { | ||
case (pos = UNIT.indexOf(char)) >= 0 && char: | ||
if ((pos = UNIT.indexOf((char = cn[0]))) >= 0) { | ||
if (number > SILVER_POS && char === GOLD) { | ||
// 万亿 | ||
char = unit | ||
} else if (pos < UNIT.indexOf(unit)) { | ||
// 替换上大的单位 | ||
if (indexUnit > TRIPLE_POS && char === UNIT_MAX) { | ||
// 万亿 | ||
char = unit | ||
} else if ((pos as any) < UNIT.indexOf(unit)) { | ||
char = unit | ||
result = result.substring(1) | ||
} else { | ||
char = '' | ||
} | ||
break | ||
|
||
default: | ||
// ZERO POINT falsy ... | ||
char = NUM.indexOf(char) > 0 ? unit + ZERO : unit | ||
char = unit | ||
cn = cn.substring(1) | ||
} else { | ||
char = '' | ||
} | ||
} else { | ||
// POINT falsy ... | ||
char = NUM.indexOf(char) > 0 ? unit + ZERO : unit | ||
} | ||
} else if (unit) { | ||
;(pos = UNIT.indexOf(result[0])) >= 0 && | ||
;(pos = UNIT.indexOf(cn[0])) >= 0 && | ||
pos < UNIT.indexOf(unit) && | ||
(result = result.substring(1)) | ||
(cn = cn.substring(1)) | ||
char += unit | ||
} | ||
} else if (indexUnit === LEN_UNIT) { | ||
indexUnit++ // 使只执行一次 | ||
result = TRIPLE + result | ||
} else if (number === LEN_UNIT) { | ||
number++ // 使只执行一次 | ||
cn = SILVER + cn | ||
} | ||
} | ||
|
||
result = char + result | ||
cn = char + cn | ||
} | ||
|
||
char = result[0] // 兼职首字 | ||
if (!char || char === POINT) { | ||
// 补零 0/0.1 | ||
result = ZERO + result | ||
} else if (char === NUM[1] && result[1] === UNIT[0]) { | ||
// 一十* => 十* | ||
result = result.substring(1) | ||
} else if (noUnit !== 2 && (char === ZERO || UNIT.indexOf(char) >= 0)) { | ||
// 去零及单位开头的 | ||
ZERO === | ||
(result = | ||
result.replace( | ||
TRIM_BEFORE, | ||
noUnit || indexUnit > LEN_UNIT ? '' : ZERO | ||
) || ZERO) && (sign = 0 as any) | ||
if (describe !== 2) { | ||
char = cn[0] // 兼职首字 | ||
if (!char || char === POINT) { | ||
cn = ZERO + cn // 补0 0/0.1 | ||
} else { | ||
if (char === ZERO || UNIT.indexOf(char) >= 0) { | ||
// 去0及单位开头的 | ||
unit = describe || number > LEN_UNIT ? '' : ZERO | ||
cn = cn.replace(TRIM_BEFORE, unit) || ZERO | ||
unit && (unit = cn[1]) && unit !== POINT && (cn = cn.substring(1)) | ||
} | ||
// 0 及 一十* => 十* | ||
ZERO === cn | ||
? (sign = 0 as any) | ||
: cases || | ||
(char === NUM[1] && cn[1] === UNIT[0] && (cn = cn.substring(1))) | ||
} | ||
} | ||
|
||
// 加正负号 | ||
return (SIGN[sign] || '') + result | ||
return (SIGN[sign] || '') + cn | ||
} | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters