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 }