Copy codeThe Code is as follows:
Function convertCurrency (currencyDigits ){
// Constants:
Var MAXIMUM_NUMBER = 99999999999.99;
// Predefine the radix characters and currency symbols for output:
Var CN_ZERO = "zero ";
Var CN_ONE = "1 ";
Var CN_TWO = "II ";
Var CN_THREE = "";
Var CN_FOUR = "Si ";
Var CN_FIVE = "Wu ";
Var CN_SIX = "";
Var CN_SEVEN = "success ";
Var CN_EIGHT = "success ";
Var CN_NINE = "canonical ";
Var CN_TEN = "pick up ";
Var CN_HUNDRED = "success ";
Var CN_THOUSAND = "Hangzhou ";
Var CN_TEN_THOUSAND = "Ten Thousand ";
Var CN_HUNDRED_MILLION = "";
Var CN_SYMBOL = "RMB ";
Var CN_DOLLAR = "RMB ";
Var CN_TEN_CENT = "";
Var CN_CENT = "points ";
Var CN_INTEGER = "whole ";
// Variables:
Var integral; // Represent integral part of digit number.
Var decimal; // Represent decimal part of digit number.
Var outputCharacters; // The output result.
Var parts;
Var digits, radices, bigRadices, decimals;
Var zeroCount;
Var I, p, d;
Var quotient, modulus;
// Validate input string:
CurrencyDigits = currencyDigits. toString ();
If (currencyDigits = ""){
// Alert ("Empty input! ");
Return "";
}
If (currencyDigits. match (/[^,. \ d]/)! = Null ){
// Alert ("Invalid characters in the input string! ");
Return "";
}
If (currencyDigits ). match (/^ (\ d {1, 3} (, \ d {3 })*(. (\ d {3},) * \ d {1, 3 }))?) | (\ D + (. \ d + )?)) $/) = Null ){
// Alert ("Illegal format of digit number! ");
Return "";
}
// Normalize the format of input digits:
CurrencyDigits = currencyDigits. replace (/,/g, ""); // Remove comma delimiters.
CurrencyDigits = currencyDigits. replace (/^ 0 +/, ""); // Trim zeros at the beginning.
// Assert the number is not greater than the maximum number.
If (Number (currencyDigits)> MAXIMUM_NUMBER ){
Alert ("the amount you entered is too large. Please enter it again! ");
Return "";
}
// Process the coversion from currency digits to characters:
// Separate integral and decimal parts before processing coversion:
Parts = currencyDigits. split (".");
If (parts. length> 1 ){
Integral = parts [0];
Decimal = parts [1];
// Cut down redundant decimal digits that are after the second.
Decimal = decimal. substr (0, 2 );
}
Else {
Integral = parts [0];
Decimal = "";
}
// Prepare the characters corresponding to the digits:
Digits = new Array (CN_ZERO, CN_ONE, CN_TWO, CN_THREE, CN_FOUR, CN_FIVE, CN_SIX, CN_SEVEN, CN_EIGHT, CN_NINE );
Radices = new Array ("", CN_TEN, CN_HUNDRED, CN_THOUSAND );
BigRadices = new Array ("", CN_TEN_THOUSAND, CN_HUNDRED_MILLION );
Decimals = new Array (CN_TEN_CENT, CN_CENT );
// Start processing:
OutputCharacters = "";
// Process integral part if it is larger than 0:
If (Number (integral)> 0 ){
ZeroCount = 0;
For (I = 0; I <integral. length; I ++ ){
P = integral. length-I-1;
D = integral. substr (I, 1 );
Quotient = p/4;
Modulus = p % 4;
If (d = "0 "){
ZeroCount ++;
}
Else {
If (zeroCount> 0)
{
OutputCharacters + = digits [0];
}
ZeroCount = 0;
OutputCharacters + = digits [Number (d)] + radices [modulus];
}
If (modulus = 0 & zeroCount <4 ){
OutputCharacters + = bigRadices [quotient];
}
}
OutputCharacters + = CN_DOLLAR;
}
// Process decimal part if there is:
If (decimal! = ""){
For (I = 0; I <decimal. length; I ++ ){
D = decimal. substr (I, 1 );
If (d! = "0 "){
OutputCharacters + = digits [Number (d)] + decimals [I];
}
}
}
// Confirm and return the final output string:
If (outputCharacters = ""){
OutputCharacters = CN_ZERO + CN_DOLLAR;
}
If (decimal = ""){
OutputCharacters + = CN_INTEGER;
}
OutputCharacters = CN_SYMBOL + outputCharacters;
Return outputCharacters;
}