Js function for converting numbers into uppercase and lowercase RMB expressions

Source: Internet
Author: User

Js function for converting numbers into uppercase and lowercase RMB expressions

There are many ways to convert numbers into large-sized RMB. This example describes how to use js to complete the conversion. Let's take a look at the following implementation code.

Function changeNumMoneyToChinese (money ){

Var cnNums = new Array ("zero", "one", "two", "three", "Si", "Wu", "Lu", "Lu ", "character", "character"); // number of Chinese Characters

Var cnIntRadice = new Array ("", "Pick", "success", "success"); // Basic Unit

Var cnIntUnits = new Array ("", "MB"); // The extended unit of the corresponding integer part

Var cnDecUnits = new Array ("", ""); // indicates the unit of the fractional part.

Var cnInteger = "integer"; // The character following the integer amount

Var cnIntLast = "RMB"; // The unit after the integer type.

Var maxNum = 999999999999999.9999; // The maximum number processed.

Var IntegerNum; // The integer part of the amount.

Var DecimalNum; // fractional part of the amount

Var ChineseStr = ""; // output Chinese amount string

Var parts; // array used to separate the amount, predefined

If (money = ""){

Return "";

}

Money = parseFloat (money );

If (money> = maxNum ){

Alert ('exceeds the maximum processing number ');

Return "";

}

If (money = 0 ){

ChineseStr = cnNums [0] + cnIntLast + cnInteger;

Return ChineseStr;

}

Money = money. toString (); // convert to a string

If (money. indexOf (".") =-1 ){

IntegerNum = money;

DecimalNum = '';

} Else {

Parts = money. split (".");

IntegerNum = parts [0];

DecimalNum = parts [1]. substr (0, 4 );

}

If (parseInt (IntegerNum, 10)> 0) {// get integer part Conversion

Var zeroCount = 0;

Var IntLen = IntegerNum. length;

For (var I = 0; I <IntLen; I ++ ){

Var n = IntegerNum. substr (I, 1 );

Var p = IntLen-I-1;

Var q = p/4;

Var m = p % 4;

If (n = "0 "){

ZeroCount ++;

} Else {

If (zeroCount> 0 ){

ChineseStr + = cnNums [0];

}

ZeroCount = 0; // return to zero

ChineseStr + = cnNums [parseInt (n)] + cnIntRadice [m];

}

If (m = 0 & zeroCount <4 ){

ChineseStr + = cnIntUnits [q];

}

}

ChineseStr + = cnIntLast;

// Integer processing is complete

}

If (DecimalNum! = '') {// Fractional part

Var decLen = DecimalNum. length;

For (var I = 0; I <decLen; I ++ ){

Var n = DecimalNum. substr (I, 1 );

If (n! = '0 '){

ChineseStr + = cnNums [Number (n)] + cnDecUnits [I];

}

}

}

If (ChineseStr = ''){

ChineseStr + = cnNums [0] + cnIntLast + cnInteger;

} Else if (DecimalNum = ''){

ChineseStr + = cnInteger;

}

Return ChineseStr;

}

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.