JS Arabic numerals to Chinese capitalization method more heavy

Source: Internet
Author: User

方法一 

 functionDX (n) {if(!/^ (0| [1-9]\d*] (\.\d+)? $/. Test (n))return"Illegal data"; varUnit = "Hundreds of thousands of hundred hundred million to pick up the corner", str = ""; N+ = "00"; varp = n.indexof ('. ')); if(P >= 0) n= n.substring (0, p) + n.substr (p+1, 2); Unit= Unit.substr (Unit.length-n.length);  for(vari=0; i < n.length; i++) Str+ = ' 0 One and three Woolu qi Ba Jiu '. CharAt (N.charat (i)) +Unit.charat (i); returnStr.replace (/0 (thousand | hundred | pick | corner)/g, "0"). Replace (/(0) +/g, "0"). Replace (/0 (million | billion | yuan)/g, "$"). Replace (/(million) million | One (pick)/g, "$1$2"). Replace (/^ $0? | 0 min/g, ""). Replace (/meta-$/g, "meta-integer"));}
View Code

方法2

<script type= "Text/javascript" >

var _change = {
ARY0: ["0", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"],
Ary1: ["", "ten", "Hundred", "thousand"],
Ary2: ["" "," Million "," billion "," 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 ("");
},//reverse 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 judgment million-level units, every four characters let the million-level unit array index number increment
i4++;
Newary = This.ary2[i4] + newary; The million-level units in the reading of the character, it must be placed at the end of the current character reading, so first stack it into the ",
Zero = ""; The "0" in the million-level unit position must not be read, so the reading of the set 0 is null

}
About the treatment and judgment of 0.
if (ary[i] = = ' 0 ') {//If the read-out character is "0", do the following to determine whether the "0" is read as "0"
Switch (i% 4) {
Case 0:
Break
If the position index can be divisible by 4, indicating that it is in the position of the million-level unit position, the position of the 0 of the reading is already set up in front, so here directly skip
Case 1:
Case 2:
Case 3:
if (ary[i-1]! = ' 0 ') {
Zero = "0"
}
; If it is not divisible by 4, then the judgment code is executed: if its next digit (which is the previous character for the current string, because it was previously reversed) is also 0, then skip, otherwise read as "0"
Break

}

Newary = zero + newary;
zero = ";
}
else {//If not "0"
Newary = This.ary0[parseint (Ary[i]) + this.ary1[i% 4] + newary; The expected character is converted into a numeric type, and as an index number of the array ary0, in order to get the corresponding Chinese reading method, followed by its first class unit (empty, ten, hundred or thousands) and finally added to the previous deposit reading content.
}

}
if (Newary.indexof ("0") = = 0) {
Newary = NEWARY.SUBSTR (1)
}//processing the previous 0
return newary;
}
}

Creating class Classes
function Change () {
This.init.apply (this, arguments);
}
Change.prototype = _change

Specific conversion use

var k = new Change ((i+1). toString ());

JS Arabic numerals to Chinese capitalization method more heavy

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.