public
Created

  • Download Gist
tochnnumbers.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
function convertToChinese(num) {
num=''+num;
var ret='', ret2='', zero='';
var nWord=['零','一','二','三','四','五','六','七','八','九','十'];
var unitWord=['','十','百','千'], unitWord2=['','萬','億','兆'];
var rWord=[[/^一十/, '十']];
var n=num.replace(/\D/g,'').split('');
for(var i=0;i<n.length;i++) {
if(Math.floor((n.length-i-1)/4)>=unitWord2.length) {
ret2+= nWord[n[i]];
continue;
}
if(n[i]==0)
zero=nWord[0];
else {
ret+= (ret==''&&ret2==''?'':zero) + nWord[n[i]] + unitWord[(n.length-i-1)%4];
zero='';
}
if((n.length-i-1)%4==0) {
if(ret!='') ret2+= ret+unitWord2[Math.floor((n.length-i-1)/4)];
zero='';
ret='';
}
}
for(var i=0;i<rWord.length;i++)
ret2=ret2.replace(rWord[i][0], rWord[i][1]);
return ret2;
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.