将金额数字转换为大写汉字的js函数
2021-07-09 15:05
标签:ast style index amp tun nes ice div 定义 将金额数字转换为大写汉字的js函数 标签:ast style index amp tun nes ice div 定义 原文地址:http://www.cnblogs.com/qiushuiblog/p/7093221.html 1 //将金额数字转换为大写汉字的函数
2 function convertCurrency(money) {
3 //汉字的数字
4 var cnNums = new Array(‘零‘, ‘壹‘, ‘贰‘, ‘叁‘, ‘肆‘, ‘伍‘, ‘陆‘, ‘柒‘, ‘捌‘, ‘玖‘);
5 //基本单位
6 var cnIntRadice = new Array(‘‘, ‘拾‘, ‘佰‘, ‘仟‘);
7 //对应整数部分扩展单位
8 var cnIntUnits = new Array(‘‘, ‘万‘, ‘亿‘, ‘兆‘);
9 //对应小数部分单位
10 var cnDecUnits = new Array(‘角‘, ‘分‘, ‘毫‘, ‘厘‘);
11 //整数金额时后面跟的字符
12 var cnInteger = ‘整‘;
13 //整型完以后的单位
14 var cnIntLast = ‘元‘;
15 //最大处理的数字
16 var maxNum = 999999999999999.9999;
17 //金额整数部分
18 var integerNum;
19 //金额小数部分
20 var decimalNum;
21 //输出的中文金额字符串
22 var chineseStr = ‘‘;
23 //分离金额后用的数组,预定义
24 var parts;
25 if (money == ‘‘) {
26 return ‘‘;
27 }
28 money = parseFloat(money);
29 if (money >= maxNum) {
30 //超出最大处理数字
31 return ‘‘;
32 }
33 if (money == 0) {
34 chineseStr = cnNums[0] + cnIntLast + cnInteger;
35 return chineseStr;
36 }
37 //转换为字符串
38 money = money.toString();
39 if (money.indexOf(‘.‘) == -1) {
40 integerNum = money;
41 decimalNum = ‘‘;
42 }
43 else {
44 parts = money.split(‘.‘);
45 integerNum = parts[0];
46 decimalNum = parts[1].substr(0, 4);
47 }
48 //获取整型部分转换
49 if (parseInt(integerNum, 10) > 0) {
50 var zeroCount = 0;
51 var IntLen = integerNum.length;
52 for (var i = 0; i ) {
53 var n = integerNum.substr(i, 1);
54 var p = IntLen - i - 1;
55 var q = p / 4;
56 var m = p % 4;
57 if (n == ‘0‘) {
58 zeroCount++;
59 }
60 else {
61 if (zeroCount > 0) {
62 chineseStr += cnNums[0];
63 }
64 //归零
65 zeroCount = 0;
66 chineseStr += cnNums[parseInt(n)] + cnIntRadice[m];
67 }
68 if (m == 0 && zeroCount ) {
69 chineseStr += cnIntUnits[q];
70 }
71 }
72 chineseStr += cnIntLast;
73 }
74 //小数部分
75 if (decimalNum != ‘‘) {
76 var decLen = decimalNum.length;
77 for (var i = 0; i ) {
78 var n = decimalNum.substr(i, 1);
79 if (n != ‘0‘) {
80 chineseStr += cnNums[Number(n)] + cnDecUnits[i];
81 }
82 }
83 }
84 if (chineseStr == ‘‘) {
85 chineseStr += cnNums[0] + cnIntLast + cnInteger;
86 }
87 else if (decimalNum == ‘‘) {
88 chineseStr += cnInteger;
89 }
90 return chineseStr;
91 }