JavaScript dynamically converts the numeric amount to the Chinese capital amount

Source: Internet
Author: User
Tags modulus

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

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.