1 function TrunToChina()
2 {
3 var num= 123456789;
4 alert(Chinese(num));
5 }
6
7 TrunToChina();
8
9
10 function Chinese(num)
11 {
12 if(!/^\d*(\.\d*)?$/.test(num))
13 {
14 alert("你输入的不是数字,请重新输入!");
15 return false;
16 }
17 var AA = new Array("零","壹","贰","叁","肆","伍","陆","柒","捌","玖");
18 var BB = new Array("","拾","佰","仟","万","亿","点","");
19 var a = (""+ num).replace(/(^0*)/g, "").split("."), k = 0, re = "";
20 for(var i=a[0].length-1; i>=0; i--)
21 {
22 switch(k)
23 {
24 case 0 :
25 re = BB[7] + re;
26 break;
27 case 4 :
28 if(!new RegExp("0{4}\\d{"+ (a[0].length-i-1) +"}$").test(a[0]))
29 re = BB[4] + re;
30 break;
31 case 8 :
32 re = BB[5] + re;
33 BB[7] = BB[5];
34 k = 0;
35 break;
36 }
37 if(k%4 == 2 && a[0].charAt(i)=="0" && a[0].charAt(i+2) != "0") re = AA[0] + re;
38 if(a[0].charAt(i) != 0) re = AA[a[0].charAt(i)] + BB[k%4] + re;
39 k++;
40 }
41 if(a.length>1) {
42 re += BB[6];
43 for(var i=0; i<a[1].length; i++) re += AA[a[1].charAt(i)];
44 }
45
46 return re;
47 }
48
2 {
3 var num= 123456789;
4 alert(Chinese(num));
5 }
6
7 TrunToChina();
8
9
10 function Chinese(num)
11 {
12 if(!/^\d*(\.\d*)?$/.test(num))
13 {
14 alert("你输入的不是数字,请重新输入!");
15 return false;
16 }
17 var AA = new Array("零","壹","贰","叁","肆","伍","陆","柒","捌","玖");
18 var BB = new Array("","拾","佰","仟","万","亿","点","");
19 var a = (""+ num).replace(/(^0*)/g, "").split("."), k = 0, re = "";
20 for(var i=a[0].length-1; i>=0; i--)
21 {
22 switch(k)
23 {
24 case 0 :
25 re = BB[7] + re;
26 break;
27 case 4 :
28 if(!new RegExp("0{4}\\d{"+ (a[0].length-i-1) +"}$").test(a[0]))
29 re = BB[4] + re;
30 break;
31 case 8 :
32 re = BB[5] + re;
33 BB[7] = BB[5];
34 k = 0;
35 break;
36 }
37 if(k%4 == 2 && a[0].charAt(i)=="0" && a[0].charAt(i+2) != "0") re = AA[0] + re;
38 if(a[0].charAt(i) != 0) re = AA[a[0].charAt(i)] + BB[k%4] + re;
39 k++;
40 }
41 if(a.length>1) {
42 re += BB[6];
43 for(var i=0; i<a[1].length; i++) re += AA[a[1].charAt(i)];
44 }
45
46 return re;
47 }
48
方法2:
function Chinese(whole){
var num;
var dig;
if(whole.indexOf(".") == -1){
num = whole;dig = "";
}else{
num = whole.substr(0,whole.indexOf("."));
dig = whole.substr( whole.indexOf(".")+1, whole.length);
};
var i=1;var len = num.length;
var dw2 = new Array("","万","亿");
var dw1 = new Array("拾","佰","千");
var dw = new Array("","壹","贰","叁","肆","伍","陆","柒","捌","玖");
var dws = new Array("零","壹","贰","叁","肆","伍","陆","柒","捌","玖");
var k1=0;
var k2=0;
var str="";
for(i=1;i<=len;i++){
var n = num.charAt(len-i);if(n=="0"){
if(k1!=0)
str = str.substr( 1, str.length-1);
}
str = dw[Number(n)].concat(str);
if(len-i-1>=0){
if(k1!=3){
str = dw1[k1].concat(str);k1++;
}else{
k1=0;;var temp = str.charAt(0);
if(temp=="万" || temp=="亿")
str = str.substr( 1, str.length-1);
str = dw2[k2].concat(str);
}
};
if(k1==3){k2++;}
};
var strdig="";
for(i=0;i<2;i++){
var n=dig.charAt(i);
strdig+=dws[Number(n)];
}
if(strdig!='零零') str += " 点 "+strdig;
return str;}
var num;
var dig;
if(whole.indexOf(".") == -1){
num = whole;dig = "";
}else{
num = whole.substr(0,whole.indexOf("."));
dig = whole.substr( whole.indexOf(".")+1, whole.length);
};
var i=1;var len = num.length;
var dw2 = new Array("","万","亿");
var dw1 = new Array("拾","佰","千");
var dw = new Array("","壹","贰","叁","肆","伍","陆","柒","捌","玖");
var dws = new Array("零","壹","贰","叁","肆","伍","陆","柒","捌","玖");
var k1=0;
var k2=0;
var str="";
for(i=1;i<=len;i++){
var n = num.charAt(len-i);if(n=="0"){
if(k1!=0)
str = str.substr( 1, str.length-1);
}
str = dw[Number(n)].concat(str);
if(len-i-1>=0){
if(k1!=3){
str = dw1[k1].concat(str);k1++;
}else{
k1=0;;var temp = str.charAt(0);
if(temp=="万" || temp=="亿")
str = str.substr( 1, str.length-1);
str = dw2[k2].concat(str);
}
};
if(k1==3){k2++;}
};
var strdig="";
for(i=0;i<2;i++){
var n=dig.charAt(i);
strdig+=dws[Number(n)];
}
if(strdig!='零零') str += " 点 "+strdig;
return str;}