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;
}