阿拉伯数字转成中文数字 (支持1~9999)

 1 <!doctype html>
 2 <html>
 3 <head>
 4 <title>xxx</title>
 5 </head>
 6 <body>
 7 <input id="test" type="text">
 8 <div id="display"></div>
 9 <script>
10 //支持到1~9999
11 function int2zh(arg_num){
12   var num = arg_num;
13   var arr1 = ['','','','','','','','','',''], arr2 = ['', '', '', '', ''];
14   var i = 0, digit, result = [];
15   while(num){
16     digit = num % 10;
17     if(digit === 0){
18       if(result.length === 0) {
19         num = Math.floor(num / 10);
20         i++;
21         continue;
22       }
23       if('' == result[result.length - 1]) {
24         num = Math.floor(num / 10);
25         i++;
26         continue;
27       }
28       result.push('');
29     }else{
30       result.push(arr2[i]);
31       result.push(arr1[digit]);
32     }
33     num = Math.floor(num / 10);
34     i++;
35   }
36   if(arg_num < 20 && arg_num >= 10){
37     result.splice(result.length - 1, 1);
38   }
39   return result.reverse().join("");
40 }
41 document.getElementById('test').addEventListener("input", function(){
42   var val = this.value;
43   var r = int2zh(val);
44   display.innerHTML = r;
45 },false);
46 </script>
47 </body>
48 </html>

 

posted on 2016-01-26 18:12  风吹树叶黄  阅读(822)  评论(0编辑  收藏  举报

导航