1、JS将阿拉伯数字翻译成中文的大写数字
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no" /> <title>JS将阿拉伯数字翻译成中文的大写数字</title> </head> <body> JS将阿拉伯数字翻译成中文的大写数字 </body> <script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.js"></script> <script type="text/javascript"> var num = 123.456; var AA = new Array("零", "一", "二", "三", "四", "五", "六", "七", "八", "九", "十"); var BB = new Array("", "十", "百", "仟", "萬", "億", "点", ""); var a = ("" + num).replace(/(^0*)/g, "").split("."), k = 0, figure = ""; for (var i = a[0].length - 1; i >= 0; i--) { switch (k) { case 0: figure = BB[7] + figure; break; case 4: if (!new RegExp("0{4}//d{" + (a[0].length - i - 1) + "}$") .test(a[0])) figure = BB[4] + figure; break; case 8: figure = BB[5] + figure; BB[7] = BB[5]; k = 0; break; } if (k % 4 == 2 && a[0].charAt(i + 2) != 0 && a[0].charAt(i + 1) == 0) figure = AA[0] + figure; if (a[0].charAt(i) != 0) figure = AA[a[0].charAt(i)] + BB[k % 4] + figure; k++; } if (a.length > 1) // 加上小数部分(如果有小数部分) { figure += BB[6]; for (var i = 0; i < a[1].length; i++) figure += AA[a[1].charAt(i)]; } if (figure == '一十') figure = "十"; if (figure.match(/^一/) && figure.length == 3) figure = figure.replace("一", ""); // return figure; console.log(figure); </script> </html>
2、JS将数字转换为大写金额
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no" /> <title>JS将数字转换为大写金额</title> </head> <body> JS将数字转换为大写金额 </body> <script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.js"></script> <script type="text/javascript"> var Num = 123.45; //判断如果传递进来的不是字符的话转换为字符 if (typeof Num == "number") { Num = new String(Num); }; Num = Num.replace(/,/g, "") //替换tomoney()中的“,” Num = Num.replace(/ /g, "") //替换tomoney()中的空格 Num = Num.replace(/¥/g, "") //替换掉可能出现的¥字符 if (isNaN(Num)) { //验证输入的字符是否为数字 //alert("请检查小写金额是否正确"); // return ""; }; //字符处理完毕后开始转换,采用前后两部分分别转换 var part = String(Num).split("."); var newchar = ""; //小数点前进行转化 for (var i = part[0].length - 1; i >= 0; i--) { if (part[0].length > 10) { // return ""; //若数量超过拾亿单位,提示 } var convert = "" var perchar = part[0].charAt(i); switch (perchar) { case "0": convert = "零" + convert; break; case "1": convert = "壹" + convert; break; case "2": convert = "贰" + convert; break; case "3": convert = "叁" + convert; break; case "4": convert = "肆" + convert; break; case "5": convert = "伍" + convert; break; case "6": convert = "陆" + convert; break; case "7": convert = "柒" + convert; break; case "8": convert = "捌" + convert; break; case "9": convert = "玖" + convert; break; } switch (part[0].length - i - 1) { case 0: convert = convert + "元"; break; case 1: if (perchar != 0) convert = convert + "拾"; break; case 2: if (perchar != 0) convert = convert + "佰"; break; case 3: if (perchar != 0) convert = convert + "仟"; break; case 4: convert = convert + "万"; break; case 5: if (perchar != 0) convert = convert + "拾"; break; case 6: if (perchar != 0) convert = convert + "佰"; break; case 7: if (perchar != 0) convert = convert + "仟"; break; case 8: convert = convert + "亿"; break; case 9: convert = convert + "拾"; break; } var newchar = convert + newchar; } //小数点之后进行转化 if (Num.indexOf(".") != -1) { if (part[1].length > 2) { // alert("小数点之后只能保留两位,系统将自动截断"); part[1] = part[1].substr(0, 2) } for (i = 0; i < part[1].length; i++) { convert = "" perchar = part[1].charAt(i) switch (perchar) { case "0": convert = "零" + convert; break; case "1": convert = "壹" + convert; break; case "2": convert = "贰" + convert; break; case "3": convert = "叁" + convert; break; case "4": convert = "肆" + convert; break; case "5": convert = "伍" + convert; break; case "6": convert = "陆" + convert; break; case "7": convert = "柒" + convert; break; case "8": convert = "捌" + convert; break; case "9": convert = "玖" + convert; break; } if (i == 0) convert = convert + "角"; if (i == 1) convert = convert + "分"; newchar = newchar + convert; } } //替换所有无用汉字 while (newchar.search("零零") != -1) newchar = newchar.replace("零零", "零"); newchar = newchar.replace("零亿", "亿"); newchar = newchar.replace("亿万", "亿"); newchar = newchar.replace("零万", "万"); newchar = newchar.replace("零元", "元"); newchar = newchar.replace("零角", ""); newchar = newchar.replace("零分", ""); if (newchar.charAt(newchar.length - 1) == "元") { newchar = newchar + "整" } // return newchar; console.log(newchar); </script> </html>
阿拉伯数字转中文-[转载-原文地址](https://blog.csdn.net/weixin_48618258/article/details/126846518) intToChinese(str) { console.log(str, 'str') str = str + ''; let len = str.length - 1; var idxs = ['', '十', '百', '千', '万', '十', '百', '千', '亿', '十', '百', '千', '万', '十', '百', '千', '亿']; var num = ['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖']; return str.replace(/([1-9]|0+)/g, function($, $1, idx, full) { var pos = 0; if ($1[0] != '0') { pos = len - idx; if (idx == 0 && $1[0] == 1 && idxs[len - idx] == '十') { return idxs[len - idx]; } return num[$1[0]] + idxs[len - idx]; } else { var left = len - idx; var right = len - idx + $1.length; if (Math.floor(right / 4) - Math.floor(left / 4) > 0) { pos = left - left % 4; } if (pos) { return idxs[pos] + num[$1[0]]; } else if (idx + $1.length >= len) { return ''; } else { return num[$1[0]] } } }); },