首页 > 代码库 > JS金额小写转大小形式
JS金额小写转大小形式
1 function convertCurrency(currencyDigits) {
2 //Constants:
3 var MAXIMUM_NUMBER = 99999999999.99;
4 //Predefine the radix characters and currency symbols for output:
5 /*
6 var CN_ZERO = "零";
7 var CN_ONE = "壹";
8 var CN_TWO = "贰";
9 var CN_THREE = "叁";
10 var CN_FOUR = "肆";
11 var CN_FIVE = "伍";
12 var CN_SIX = "陆";
13 var CN_SEVEN = "柒";
14 var CN_EIGHT = "捌";
15 var CN_NINE = "玖";
16 var CN_TEN = "拾";
17 var CN_HUNDRED = "佰";
18 var CN_THOUSAND = "仟";
19 var CN_TEN_THOUSAND = "万";
20 var CN_HUNDRED_MILLION = "亿";
21 var CN_SYMBOL = "人民币";
22 var CN_DOLLAR = "元";
23 var CN_TEN_CENT = "角";
24 var CN_CENT = "分";
25 var CN_INTEGER = "整";
26 */
27
28 var CN_ZERO = "零";
29 var CN_ONE = "一";
30 var CN_TWO = "二";
31 var CN_THREE = "三";
32 var CN_FOUR = "四";
33 var CN_FIVE = "五";
34 var CN_SIX = "六";
35 var CN_SEVEN = "七";
36 var CN_EIGHT = "八";
37 var CN_NINE = "九";
38 var CN_TEN = "十";
39 var CN_HUNDRED = "百";
40 var CN_THOUSAND = "千";
41 var CN_TEN_THOUSAND = "万";
42 var CN_HUNDRED_MILLION = "亿";
43 var CN_SYMBOL = "人民币";
44 var CN_DOLLAR = "元";
45 var CN_TEN_CENT = "角";
46 var CN_CENT = "分";
47 var CN_INTEGER = "整";
48
49 //Variables:
50 var integral;
51 //Represent integral part of digit number.
52 var decimal;
53 //Represent decimal part of digit number.
54 var outputCharacters;
55 //The output result.
56 var parts;
57 var digits, radices, bigRadices, decimals;
58 var zeroCount;
59 var i, p, d;
60 var quotient, modulus;
61 //Validate input string:
62 currencyDigits = currencyDigits.toString();
63 if (currencyDigits == "") { alert("Empty input!"); return ""; }
64 if (currencyDigits.match(/[^,.\d]/) != null) { alert("Invalid characters in the input string!"); return ""; }
65 if ((currencyDigits).match(/^((\d{1,3}(,\d{3})*(.((\d{3},)*\d{1,3}))?)|(\d+(.\d+)?))$/) == null) { alert("Illegal format of digit number!"); return ""; }
66 //Normalize the format of input digits:
67 currencyDigits = currencyDigits.replace(/,/g, "");
68 //Remove comma delimiters.
69 currencyDigits = currencyDigits.replace(/^0+/, "");
70 //Trim zeros at the beginning.//Assert the number is not greater than the maximum number.
71 if (Number(currencyDigits) > MAXIMUM_NUMBER) { alert("Too large a number to convert!"); return ""; }
72 //Process the coversion from currency digits to characters:
73 //Separate integral and decimal parts before processing coversion:
74 parts = currencyDigits.split(".");
75 if (parts.length > 1) {
76 integral = parts[0]; decimal = parts[1];
77 //Cut down redundant decimal digits that are after the second.
78 decimal = decimal.substr(0, 2);
79 } else { integral = parts[0]; decimal = ""; }
80 //Prepare the characters corresponding to the digits:
81 digits = new Array(CN_ZERO, CN_ONE, CN_TWO, CN_THREE, CN_FOUR, CN_FIVE, CN_SIX, CN_SEVEN, CN_EIGHT, CN_NINE);
82 radices = new Array("", CN_TEN, CN_HUNDRED, CN_THOUSAND);
83 bigRadices = new Array("", CN_TEN_THOUSAND, CN_HUNDRED_MILLION);
84 decimals = new Array(CN_TEN_CENT, CN_CENT);
85 //Start processing:
86 outputCharacters = "";
87 //Process integral part if it is larger than 0:
88 if (Number(integral) > 0) {
89 zeroCount = 0; for (i = 0; i < integral.length; i++) {
90 p = integral.length - i - 1; d = integral.substr(i, 1); quotient = p / 4; modulus = p % 4; if (d == "0") { zeroCount++; }
91 else {
92 if (zeroCount > 0) { outputCharacters += digits[0]; } zeroCount = 0; outputCharacters += digits[Number(d)] + radices[modulus];
93 }
94 if (modulus == 0 && zeroCount < 4) { outputCharacters += bigRadices[quotient]; }
95 }
96 outputCharacters += CN_DOLLAR;
97 }
98 //Process decimal part if there is:
99 if (decimal != "") {
100 for (i = 0; i < decimal.length; i++) {
101 d = decimal.substr(i, 1);
102 if (d != "0") { outputCharacters += digits[Number(d)] + decimals[i]; }
103 }
104 }
105 //Confirm and return the final output string:
106 if (outputCharacters == "") { outputCharacters = CN_ZERO + CN_DOLLAR; }
107 if (decimal == "") { outputCharacters += CN_INTEGER; }
108 outputCharacters =CN_SYMBOL + " " + outputCharacters;
109 return outputCharacters;
110 }
2 //Constants:
3 var MAXIMUM_NUMBER = 99999999999.99;
4 //Predefine the radix characters and currency symbols for output:
5 /*
6 var CN_ZERO = "零";
7 var CN_ONE = "壹";
8 var CN_TWO = "贰";
9 var CN_THREE = "叁";
10 var CN_FOUR = "肆";
11 var CN_FIVE = "伍";
12 var CN_SIX = "陆";
13 var CN_SEVEN = "柒";
14 var CN_EIGHT = "捌";
15 var CN_NINE = "玖";
16 var CN_TEN = "拾";
17 var CN_HUNDRED = "佰";
18 var CN_THOUSAND = "仟";
19 var CN_TEN_THOUSAND = "万";
20 var CN_HUNDRED_MILLION = "亿";
21 var CN_SYMBOL = "人民币";
22 var CN_DOLLAR = "元";
23 var CN_TEN_CENT = "角";
24 var CN_CENT = "分";
25 var CN_INTEGER = "整";
26 */
27
28 var CN_ZERO = "零";
29 var CN_ONE = "一";
30 var CN_TWO = "二";
31 var CN_THREE = "三";
32 var CN_FOUR = "四";
33 var CN_FIVE = "五";
34 var CN_SIX = "六";
35 var CN_SEVEN = "七";
36 var CN_EIGHT = "八";
37 var CN_NINE = "九";
38 var CN_TEN = "十";
39 var CN_HUNDRED = "百";
40 var CN_THOUSAND = "千";
41 var CN_TEN_THOUSAND = "万";
42 var CN_HUNDRED_MILLION = "亿";
43 var CN_SYMBOL = "人民币";
44 var CN_DOLLAR = "元";
45 var CN_TEN_CENT = "角";
46 var CN_CENT = "分";
47 var CN_INTEGER = "整";
48
49 //Variables:
50 var integral;
51 //Represent integral part of digit number.
52 var decimal;
53 //Represent decimal part of digit number.
54 var outputCharacters;
55 //The output result.
56 var parts;
57 var digits, radices, bigRadices, decimals;
58 var zeroCount;
59 var i, p, d;
60 var quotient, modulus;
61 //Validate input string:
62 currencyDigits = currencyDigits.toString();
63 if (currencyDigits == "") { alert("Empty input!"); return ""; }
64 if (currencyDigits.match(/[^,.\d]/) != null) { alert("Invalid characters in the input string!"); return ""; }
65 if ((currencyDigits).match(/^((\d{1,3}(,\d{3})*(.((\d{3},)*\d{1,3}))?)|(\d+(.\d+)?))$/) == null) { alert("Illegal format of digit number!"); return ""; }
66 //Normalize the format of input digits:
67 currencyDigits = currencyDigits.replace(/,/g, "");
68 //Remove comma delimiters.
69 currencyDigits = currencyDigits.replace(/^0+/, "");
70 //Trim zeros at the beginning.//Assert the number is not greater than the maximum number.
71 if (Number(currencyDigits) > MAXIMUM_NUMBER) { alert("Too large a number to convert!"); return ""; }
72 //Process the coversion from currency digits to characters:
73 //Separate integral and decimal parts before processing coversion:
74 parts = currencyDigits.split(".");
75 if (parts.length > 1) {
76 integral = parts[0]; decimal = parts[1];
77 //Cut down redundant decimal digits that are after the second.
78 decimal = decimal.substr(0, 2);
79 } else { integral = parts[0]; decimal = ""; }
80 //Prepare the characters corresponding to the digits:
81 digits = new Array(CN_ZERO, CN_ONE, CN_TWO, CN_THREE, CN_FOUR, CN_FIVE, CN_SIX, CN_SEVEN, CN_EIGHT, CN_NINE);
82 radices = new Array("", CN_TEN, CN_HUNDRED, CN_THOUSAND);
83 bigRadices = new Array("", CN_TEN_THOUSAND, CN_HUNDRED_MILLION);
84 decimals = new Array(CN_TEN_CENT, CN_CENT);
85 //Start processing:
86 outputCharacters = "";
87 //Process integral part if it is larger than 0:
88 if (Number(integral) > 0) {
89 zeroCount = 0; for (i = 0; i < integral.length; i++) {
90 p = integral.length - i - 1; d = integral.substr(i, 1); quotient = p / 4; modulus = p % 4; if (d == "0") { zeroCount++; }
91 else {
92 if (zeroCount > 0) { outputCharacters += digits[0]; } zeroCount = 0; outputCharacters += digits[Number(d)] + radices[modulus];
93 }
94 if (modulus == 0 && zeroCount < 4) { outputCharacters += bigRadices[quotient]; }
95 }
96 outputCharacters += CN_DOLLAR;
97 }
98 //Process decimal part if there is:
99 if (decimal != "") {
100 for (i = 0; i < decimal.length; i++) {
101 d = decimal.substr(i, 1);
102 if (d != "0") { outputCharacters += digits[Number(d)] + decimals[i]; }
103 }
104 }
105 //Confirm and return the final output string:
106 if (outputCharacters == "") { outputCharacters = CN_ZERO + CN_DOLLAR; }
107 if (decimal == "") { outputCharacters += CN_INTEGER; }
108 outputCharacters =CN_SYMBOL + " " + outputCharacters;
109 return outputCharacters;
110 }
JS金额小写转大小形式
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。