阿拉伯數字轉中文大寫數字的JS
阿新 • • 發佈:2018-01-23
replace lac 16px fun repl span 大寫 font func
1 function intToChinese ( str ) {
2 str = str+‘‘;
3 var len = str.length-1;
4 var idxs = [‘‘,‘十‘,‘百‘,‘千‘,‘萬‘,‘十‘,‘百‘,‘千‘,‘億‘,‘十‘,‘百‘,‘千‘,‘萬‘,‘十‘,‘百‘,‘千‘,‘億‘];
5 var num = [‘零‘,‘壹‘,‘貳‘,‘叁‘,‘肆‘,‘伍‘,‘陸‘,‘柒‘,‘捌‘,‘玖‘];
6 return str.replace(/([1-9]|0+)/g,function( $, $1, idx, full) {
7 var pos = 0;
8 if( $1[0] != ‘0‘ ){
9 pos = len-idx;
10 if( idx == 0 && $1[0] == 1 && idxs[len-idx] == ‘十‘){
11 return idxs[len-idx];
12 }
13 return num[$1[0]] + idxs[len-idx];
14 } else {
15 var left = len - idx;
16 var right = len - idx + $1.length;
17 if( Math.floor(right/4) - Math.floor(left/4) > 0 ){
18 pos = left - left%4;
19 }
20 if( pos ){
21 return idxs[pos] + num[$1[0]];
22 } else if( idx + $1.length >= len ){
23 return ‘‘;
24 }else {
25 return num[$1[0]]
26 }
27 }
28 });
29 }
阿拉伯數字轉中文大寫數字的JS