数字转大写
1
<!
DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
>
2 < html xmlns ="http://www.w3.org/1999/xhtml" >
3 < head >
4 < meta http-equiv ="Content-Type" content ="text/html; charset=gb2312" />
5 < title ></ title >
6 < script language ="jscript" >
7 function convertCurrency(currencyDigits) {
8 // Constants:
9 var MAXIMUM_NUMBER = 99999999999.99 ;
10 // Predefine the radix characters and currency symbols for output:
11 var CN_ZERO = " 零 " ;
12 var CN_ONE = " 壹 " ;
13 var CN_TWO = " 贰 " ;
14 var CN_THREE = " 叁 " ;
15 var CN_FOUR = " 肆 " ;
16 var CN_FIVE = " 伍 " ;
17 var CN_SIX = " 陆 " ;
18 var CN_SEVEN = " 柒 " ;
19 var CN_EIGHT = " 捌 " ;
20 var CN_NINE = " 玖 " ;
21 var CN_TEN = " 拾 " ;
22 var CN_HUNDRED = " 佰 " ;
23 var CN_THOUSAND = " 仟 " ;
24 var CN_TEN_THOUSAND = " 万 " ;
25 var CN_HUNDRED_MILLION = " 亿 " ;
26 var CN_SYMBOL = " 人民币 " ;
27 var CN_DOLLAR = " 元 " ;
28 var CN_TEN_CENT = " 角 " ;
29 var CN_CENT = " 分 " ;
30 var CN_INTEGER = " 整 " ;
31
32 // Variables:
33 var integral; // Represent integral part of digit number.
34 var decimal; // Represent decimal part of digit number.
35 var outputCharacters; // The output result.
36 var parts;
37 var digits, radices, bigRadices, decimals;
38 var zeroCount;
39 var i, p, d;
40 var quotient, modulus;
41
42 // Validate input string:
43 currencyDigits = currencyDigits.toString();
44 if (currencyDigits == "" ) {
45 alert( " Empty input! " );
46 return "" ;
47 }
48 if (currencyDigits.match( / [^,.\d] / ) != null ) {
49 alert( " Invalid characters in the input string! " );
50 return "" ;
51 }
52 if ((currencyDigits).match( / ^((\d{1,3}(,\d{3})*(.((\d{3},)*\d{1,3}))?)|(\d+(.\d+)?))$ / ) == null ) {
53 alert( " Illegal format of digit number! " );
54 return "" ;
55 }
56
57 // Normalize the format of input digits:
58 currencyDigits = currencyDigits.replace( / , / g, "" ); // Remove comma delimiters.
59 currencyDigits = currencyDigits.replace( / ^0+ / , "" ); // Trim zeros at the beginning.
60 // Assert the number is not greater than the maximum number.
61 if (Number(currencyDigits) > MAXIMUM_NUMBER) {
62 alert( " Too large a number to convert! " );
63 return "" ;
64 }
65
66 // Process the coversion from currency digits to characters:
67 // Separate integral and decimal parts before processing coversion:
68 parts = currencyDigits.split( " . " );
69 if (parts.length > 1 ) {
70 integral = parts[ 0 ];
71 decimal = parts[ 1 ];
72 // Cut down redundant decimal digits that are after the second.
73 decimal = decimal.substr( 0 , 2 );
74 }
75 else {
76 integral = parts[ 0 ];
77 decimal = "" ;
78 }
79 // Prepare the characters corresponding to the digits:
80 digits = new Array(CN_ZERO, CN_ONE, CN_TWO, CN_THREE, CN_FOUR, CN_FIVE, CN_SIX, CN_SEVEN, CN_EIGHT, CN_NINE);
81 radices = new Array( "" , CN_TEN, CN_HUNDRED, CN_THOUSAND);
82 bigRadices = new Array( "" , CN_TEN_THOUSAND, CN_HUNDRED_MILLION);
83 decimals = new Array(CN_TEN_CENT, CN_CENT);
84 // Start processing:
85 outputCharacters = "" ;
86 // Process integral part if it is larger than 0:
87 if (Number(integral) > 0 ) {
88 zeroCount = 0 ;
89 for (i = 0 ; i < integral.length; i ++ ) {
90 p = integral.length - i - 1 ;
91 d = integral.substr(i, 1 );
92 quotient = p / 4 ;
93 modulus = p % 4 ;
94 if (d == " 0 " ) {
95 zeroCount ++ ;
96 }
97 else {
98 if (zeroCount > 0 )
99 {
100 outputCharacters += digits[ 0 ];
101 }
102 zeroCount = 0 ;
103 outputCharacters += digits[Number(d)] + radices[modulus];
104 }
105 if (modulus == 0 && zeroCount < 4 ) {
106 outputCharacters += bigRadices[quotient];
107 }
108 }
109 outputCharacters += CN_DOLLAR;
110 }
111 // Process decimal part if there is:
112 if (decimal != "" ) {
113 for (i = 0 ; i < decimal.length; i ++ ) {
114 d = decimal.substr(i, 1 );
115 if (d != " 0 " ) {
116 outputCharacters += digits[Number(d)] + decimals[i];
117 }
118 }
119 }
120 // Confirm and return the final output string:
121 if (outputCharacters == "" ) {
122 outputCharacters = CN_ZERO + CN_DOLLAR;
123 }
124 if (decimal == "" ) {
125 outputCharacters += CN_INTEGER;
126 }
127 outputCharacters = CN_SYMBOL + outputCharacters;
128 return outputCharacters;
129 }
130 </ script >
131 </ head >
132 < body >
133 < INPUT id ="Digits" type ="text" name ="Digits" size =20 >
134 < INPUT id ="Convert" type ="button" value ="Convert" name ="Convert" onclick ="Result.value = convertCurrency(Digits.value);" >
135 < INPUT id ="Result" type ="text" name ="Result" size =60 >
136 </ body >
137 </ html >
2 < html xmlns ="http://www.w3.org/1999/xhtml" >
3 < head >
4 < meta http-equiv ="Content-Type" content ="text/html; charset=gb2312" />
5 < title ></ title >
6 < script language ="jscript" >
7 function convertCurrency(currencyDigits) {
8 // Constants:
9 var MAXIMUM_NUMBER = 99999999999.99 ;
10 // Predefine the radix characters and currency symbols for output:
11 var CN_ZERO = " 零 " ;
12 var CN_ONE = " 壹 " ;
13 var CN_TWO = " 贰 " ;
14 var CN_THREE = " 叁 " ;
15 var CN_FOUR = " 肆 " ;
16 var CN_FIVE = " 伍 " ;
17 var CN_SIX = " 陆 " ;
18 var CN_SEVEN = " 柒 " ;
19 var CN_EIGHT = " 捌 " ;
20 var CN_NINE = " 玖 " ;
21 var CN_TEN = " 拾 " ;
22 var CN_HUNDRED = " 佰 " ;
23 var CN_THOUSAND = " 仟 " ;
24 var CN_TEN_THOUSAND = " 万 " ;
25 var CN_HUNDRED_MILLION = " 亿 " ;
26 var CN_SYMBOL = " 人民币 " ;
27 var CN_DOLLAR = " 元 " ;
28 var CN_TEN_CENT = " 角 " ;
29 var CN_CENT = " 分 " ;
30 var CN_INTEGER = " 整 " ;
31
32 // Variables:
33 var integral; // Represent integral part of digit number.
34 var decimal; // Represent decimal part of digit number.
35 var outputCharacters; // The output result.
36 var parts;
37 var digits, radices, bigRadices, decimals;
38 var zeroCount;
39 var i, p, d;
40 var quotient, modulus;
41
42 // Validate input string:
43 currencyDigits = currencyDigits.toString();
44 if (currencyDigits == "" ) {
45 alert( " Empty input! " );
46 return "" ;
47 }
48 if (currencyDigits.match( / [^,.\d] / ) != null ) {
49 alert( " Invalid characters in the input string! " );
50 return "" ;
51 }
52 if ((currencyDigits).match( / ^((\d{1,3}(,\d{3})*(.((\d{3},)*\d{1,3}))?)|(\d+(.\d+)?))$ / ) == null ) {
53 alert( " Illegal format of digit number! " );
54 return "" ;
55 }
56
57 // Normalize the format of input digits:
58 currencyDigits = currencyDigits.replace( / , / g, "" ); // Remove comma delimiters.
59 currencyDigits = currencyDigits.replace( / ^0+ / , "" ); // Trim zeros at the beginning.
60 // Assert the number is not greater than the maximum number.
61 if (Number(currencyDigits) > MAXIMUM_NUMBER) {
62 alert( " Too large a number to convert! " );
63 return "" ;
64 }
65
66 // Process the coversion from currency digits to characters:
67 // Separate integral and decimal parts before processing coversion:
68 parts = currencyDigits.split( " . " );
69 if (parts.length > 1 ) {
70 integral = parts[ 0 ];
71 decimal = parts[ 1 ];
72 // Cut down redundant decimal digits that are after the second.
73 decimal = decimal.substr( 0 , 2 );
74 }
75 else {
76 integral = parts[ 0 ];
77 decimal = "" ;
78 }
79 // Prepare the characters corresponding to the digits:
80 digits = new Array(CN_ZERO, CN_ONE, CN_TWO, CN_THREE, CN_FOUR, CN_FIVE, CN_SIX, CN_SEVEN, CN_EIGHT, CN_NINE);
81 radices = new Array( "" , CN_TEN, CN_HUNDRED, CN_THOUSAND);
82 bigRadices = new Array( "" , CN_TEN_THOUSAND, CN_HUNDRED_MILLION);
83 decimals = new Array(CN_TEN_CENT, CN_CENT);
84 // Start processing:
85 outputCharacters = "" ;
86 // Process integral part if it is larger than 0:
87 if (Number(integral) > 0 ) {
88 zeroCount = 0 ;
89 for (i = 0 ; i < integral.length; i ++ ) {
90 p = integral.length - i - 1 ;
91 d = integral.substr(i, 1 );
92 quotient = p / 4 ;
93 modulus = p % 4 ;
94 if (d == " 0 " ) {
95 zeroCount ++ ;
96 }
97 else {
98 if (zeroCount > 0 )
99 {
100 outputCharacters += digits[ 0 ];
101 }
102 zeroCount = 0 ;
103 outputCharacters += digits[Number(d)] + radices[modulus];
104 }
105 if (modulus == 0 && zeroCount < 4 ) {
106 outputCharacters += bigRadices[quotient];
107 }
108 }
109 outputCharacters += CN_DOLLAR;
110 }
111 // Process decimal part if there is:
112 if (decimal != "" ) {
113 for (i = 0 ; i < decimal.length; i ++ ) {
114 d = decimal.substr(i, 1 );
115 if (d != " 0 " ) {
116 outputCharacters += digits[Number(d)] + decimals[i];
117 }
118 }
119 }
120 // Confirm and return the final output string:
121 if (outputCharacters == "" ) {
122 outputCharacters = CN_ZERO + CN_DOLLAR;
123 }
124 if (decimal == "" ) {
125 outputCharacters += CN_INTEGER;
126 }
127 outputCharacters = CN_SYMBOL + outputCharacters;
128 return outputCharacters;
129 }
130 </ script >
131 </ head >
132 < body >
133 < INPUT id ="Digits" type ="text" name ="Digits" size =20 >
134 < INPUT id ="Convert" type ="button" value ="Convert" name ="Convert" onclick ="Result.value = convertCurrency(Digits.value);" >
135 < INPUT id ="Result" type ="text" name ="Result" size =60 >
136 </ body >
137 </ html >