The JavaScript implementation converts Numbers into Chinese

  • 2020-06-22 23:48:09
  • OfStack


var _change = {
           ary0:[" zero ", "1", "2", "3", "4", "5", "6", "7", "8", "9"],
           ary1:["", "10", " best ", " thousand "],
           ary2:["", " wan ", " Hundred million ", " mega "],
           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("");
           }, // Invert the string. 
           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) { // First judge the 10,000 level units, every interval 4 Six characters increments the index number of the ten-thousand unit array 
                       i4++;
                       newary = this.ary2[i4] + newary; // Store the 10,000-level units in the reading of the character, which must be placed at the end of the current character reading, so fold it in first $r , 
                       zero = ""; // At the level of 10,000 units" 0 "Is definitely not to be read, so set zero to be read null 
 
                   }
                   // about 0 Processing and judgment. 
                   if (ary[i] == '0') { // If the character read is" 0 ", perform the following judgment of this" 0 "Read as" zero" 
                       switch (i % 4) {
                           case 0:
                               break;
                           // If the position index can be 4 Divisible means that it's in a 10,000-level unit position 0 ", so I'll skip it 
                           case 1:
                           case 2:
                           case 3:
                               if (ary[i - 1] != '0') {
                                   zero = " zero "
                               }
                               ; // If you don't be 4 Divisible, then all execute this judgment code: if it's below 1 Bits (up for the current string) 1 Two characters, since the inversion was performed earlier) 0 , then skip, otherwise read as "zero" 
                               break;
 
                       }
 
                       newary = zero + newary;
                       zero = '';
                   }
                   else { // If not," 0 " 
                       newary = this.ary0[parseInt(ary[i])] + this.ary1[i % 4] + newary; // Converts the character to a numeric type , And as an array ary0 The index number of , To get the corresponding Chinese pronunciation, and then to follow it 1 Grade unit (empty, 10.  Hundreds or thousands) plus the previous stored reading content. 
                   }
 
               }
               if (newary.indexOf(" zero ") == 0) {
                   newary = newary.substr(1)
               }// Deal with the preceding 0
               return newary;
           }
       }
 
       // create class class 
       function change() {
           this.init.apply(this, arguments);
       }
       change.prototype = _change
 
// Create an instance 
       var k = new change("00102040");
       alert(k.pri_ary())

Related articles: