Example code of converting numbers into Chinese Characters in js

Source: Internet
Author: User

Js converts a number into an example code of a Chinese character and directly references the following code. If you have any questions, please leave a message.

Copy to ClipboardReference: [www.bkjia.com] var _ change = {
Ary0: ["zero", "one", "two", "three", "four", "five", "Six", "Seven", "eight ", "9"],
Ary1: ["", "Ten", "Hundred", "Thousand"],
Ary2: ["," MB "],
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 ("");
}, // Reverse 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 ten-level unit. Every four characters increase the index number of the TEN-level unit array.
I4 ++;
Newary = this. ary2 [i4] + newary; // stores tens of thousands of units in the read method of the character. It must be placed at the end of the read method of the current character, so it is first stacked into $ r,
Zero = ""; // The "0" in the unit of 10 million must be read, so the zero read method is set to null.

}
// Processing and judgment of 0.
If (ary [I] = '0') {// if the read character is "0", run the following command to determine whether the "0" is read as "0"
Switch (I % 4 ){
Case 0:
Break;
// If the location index can be fully divided by four, it indicates that it is in the unit of tens of thousands. The 0 reading method at this location is already set in front, so skip this step directly.
Case 1:
Case 2:
Case 3:
If (ary [I-1]! = '0 '){
Zero = "zero"
}
; // If it is not divisible by four, execute this code: If it is the next digit (for the current string, it is the previous character, because it was previously reversed) if it is also 0, skip this step. Otherwise, read it as "0"
Break;

}

Newary = zero + newary;
Zero = '';
}
Else {// if it is not "0"
Newary = this. ary0 [parseInt (ary [I])] + this. ary1 [I % 4] + newary; // converts the character to a numeric value and uses it as the index number of the array ary0 to obtain the corresponding Chinese reading method, then, it will keep up with its level 1 unit (null, ten, hundred or thousands) and add the previously saved read content.
}

}
If (newary. indexOf ("0") = 0 ){
Newary = newary. substr (1)
} // Process the preceding 0
Return newary;
}
}

// Create a class
Function change (){
This. init. apply (this, arguments );
}
Change. prototype = _ change

// Create an instance
Var k = new changes ("00102040 ");
Alert (k. pri_ary ())

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.