A function is written in JavaScript to complete the following functions:
1. convert a given string, for example, 1234.55, to a correct Chinese currency Description: for example, RMB
2. The input string can be in the following forms: With separators, for example, 123,456,789.00; without separators, for example: 123456789
3. The output description of the Chinese currency must comply with the specifications, such as: 0.3 ---- the renminbi triangle; 0.33 ---- the renminbi triangle, and 1 ---- the Renminbi
RMB 100 ---- RMB 1 million CNY; RMB 1001 ---- RMB 1 million CNY 1 million CNY 1 million CNY 2 million RMB 1 million CNY 1 million CNY 2 million RMB 1 million CNY 1 million CNY 2 million CNY 1 million CNY 1 million CNY 2 million CNY 1 million CNY 2 million CNY 1 million CNY 2 million CNY 1 million CNY 2 million CNY 1;
RMB 1001001 ---- RMB million CNY and RMB million CNY
4. The maximum conversion speed is 10 billion.
SpecificCodeAs follows:
<! Doctype HTML public "-// W3C // dtd html 4.0 transitional // en">
<HTML>
<Head>
<Title> </title>
<Script language = "jscript">
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 ("too large a number to convert! ");
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;
}
</SCRIPT>
</Head>
<Body>
<Input id = "digits" type = "text" name = "digits" size = 20>
<Input id = "convert" type = "button" value = "convert" name = "convert" onclick = "result. value = convertcurrency (digits. Value);">
<Input id = "result" type = "text" name = "result" size = 60>
</Body>
</Html>
The above Code uses the regular expression function, in addition to the correct conversion to the capital form of the renminbi, you can also determine whether the input form is correct and valid.