DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> JavaScript入門知識 >> 關於JavaScript >> JavaScript實現把數字轉換成中文
JavaScript實現把數字轉換成中文
編輯:關於JavaScript     
var _change = {
           ary0:["零", "一", "二", "三", "四", "五", "六", "七", "八", "九"],
           ary1:["", "十", "百", "千"],
           ary2:["", "萬", "億", "兆"],
           init:function (name) {
               this.name = name;
           },
           strrev:function () {
               var ary = []
               for (var i = this.name.length; i >= 0; i--) {
                   ary.push(this.name[i])
               }
               return ary.join("");
           }, //倒轉字符串。
           pri_ary:function () {
               var $this = this
               var ary = this.strrev();
               var zero = ""
               var newary = ""
               var i4 = -1
               for (var i = 0; i < ary.length; i++) {
                   if (i % 4 == 0) { //首先判斷萬級單位,每隔四個字符就讓萬級單位數組索引號遞增
                       i4++;
                       newary = this.ary2[i4] + newary; //將萬級單位存入該字符的讀法中去,它肯定是放在當前字符讀法的末尾,所以首先將它疊加入$r中,
                       zero = ""; //在萬級單位位置的“0”肯定是不用的讀的,所以設置零的讀法為空
 
                   }
                   //關於0的處理與判斷。
                   if (ary[i] == '0') { //如果讀出的字符是“0”,執行如下判斷這個“0”是否讀作“零”
                       switch (i % 4) {
                           case 0:
                               break;
                           //如果位置索引能被4整除,表示它所處位置是萬級單位位置,這個位置的0的讀法在前面就已經設置好了,所以這裡直接跳過
                           case 1:
                           case 2:
                           case 3:
                               if (ary[i - 1] != '0') {
                                   zero = "零"
                               }
                               ; //如果不被4整除,那麼都執行這段判斷代碼:如果它的下一位數字(針對當前字符串來說是上一個字符,因為之前執行了反轉)也是0,那麼跳過,否則讀作“零”
                               break;
 
                       }
 
                       newary = zero + newary;
                       zero = '';
                   }
                   else { //如果不是“0”
                       newary = this.ary0[parseInt(ary[i])] + this.ary1[i % 4] + newary; //就將該當字符轉換成數值型,並作為數組ary0的索引號,以得到與之對應的中文讀法,其後再跟上它的的一級單位(空、十、百還是千)最後再加上前面已存入的讀法內容。
                   }
 
               }
               if (newary.indexOf("零") == 0) {
                   newary = newary.substr(1)
               }//處理前面的0
               return newary;
           }
       }
 
       //創建class類
       function change() {
           this.init.apply(this, arguments);
       }
       change.prototype = _change
 
//創建實例
       var k = new change("00102040");
       alert(k.pri_ary())
XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved