JavaScript code for converting amounts (the input Arabic numerals) into Chinese

Source: Internet
Author: User

Copy codeThe Code is as follows:
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312"/>
<Title> untitled document </title>
<Script type = "text/javascript" language = "javascript">
Function clickchange (obj ){
Var aaval = obj. value;
Alert (aaval );
Document. getElementById ("bb"). value = Chinaco ST (aaval );
}

Function Chinaco ST (numberValue ){
Var numberValue = new String (Math. round (numberValue * 100); // numeric amount
Var chineseValue = ""; // converted Chinese Character amount
Var String1 = ""; // Chinese Character number
Var String2 = "tens of thousands of RMB, hundreds of millions of RMB, and tens of thousands of RMB"; // corresponding unit
Var len = numberValue. length; // the string length of numberValue
Var reading; // Chinese reading of numbers
Var Ch2; // Chinese character reading of digits
Var nZero = 0; // used to calculate the number of consecutive zero values
Var String3; // the value at the specified position
If (len> 15 ){
Alert ("beyond calculation range ");
Return "";
}
If (numberValue = 0 ){
ChineseValue = "Zero RMB ";
Return chineseValue;
}

String2 = String2.substr (String2.length-len, len); // retrieve the value of STRING2 in the corresponding number of digits
For (var I = 0; I <len; I ++ ){
String3 = parseInt (numberValue. substr (I, 1), 10); // retrieve the value of a certain bit to be converted
If (I! = (Len-3) & I! = (Len-7) & I! = (Len-11) & I! = (Len-15 )){
If (String3 = 0 ){
Operator = "";
Ch2 = "";
NZero = nZero + 1;
}
Else if (String3! = 0 & nZero! = 0 ){
Substring = "zero" + String1.substr (String3, 1 );
Ch2 = String2.substr (I, 1 );
NZero = 0;
}
Else {
Substring = String1.substr (String3, 1 );
Ch2 = String2.substr (I, 1 );
NZero = 0;
}
}
Else {// This digit is a key digit of trillions, hundreds of millions, tens of thousands, and Yuan.
If (String3! = 0 & nZero! = 0 ){
Substring = "zero" + String1.substr (String3, 1 );
Ch2 = String2.substr (I, 1 );
NZero = 0;
}
Else if (String3! = 0 & nZero = 0 ){
Substring = String1.substr (String3, 1 );
Ch2 = String2.substr (I, 1 );
NZero = 0;
}
Else if (String3 = 0 & nZero> = 3 ){
Operator = "";
Ch2 = "";
NZero = nZero + 1;
}
Else {
Operator = "";
Ch2 = String2.substr (I, 1 );
NZero = nZero + 1;
}
If (I = (len-11) | I = (len-3) {// if this bit is a hundred million or a metaspace, you must enter
Ch2 = String2.substr (I, 1 );
}
}
ChineseValue = chineseValue + interval + Ch2;
}

If (String3 = 0) {// when the last digit (minute) is 0, add "integer"
ChineseValue = chineseValue + "integer ";
}

Return chineseValue;
}
</Script>
</Head>

<Body>
<Input type = "text" id = "aa" onblur = "clickchange (this)"/>
<Input type = "text" id = "bb"/>
</Body>
</Html>
 

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.