JS函数实现金额小写转大写

止乎于分享!

 1     ///<summery>小写金额转化大写金额</summery>
 2     function AmountLtoU(amount) {
 3         if (isNaN(amount) || amount >= 1000000000000) return "无效金额!";  //数值最大不超过1万亿
 4         var sPrefix = amount < 0 ? "(负)" : "";                             //将负号‘-’显示成汉字‘(负)’
 5         var sAmount = Math.abs(amount).toFixed(2);                          //格式化成两位小数格式(两位小数对应“‘角”和“分”)
 6         var sUnit = ‘仟佰拾亿仟佰拾万仟佰拾元角分‘;                         //14个权位单位
 7         var sCapital = ‘零壹贰叁肆伍陆柒捌玖‘;                              //10个大写数字
 8         sAmount = sAmount.replace(".", "");                                 //去除小数点(使数字与权位对应)
 9         sUnit = sUnit.substr(sUnit.length - sAmount.length);                //截取权位
10         var sOutput = "";  
11         for (var i = 0, len = sAmount.length; i < len; i++) {               //拼接数字与权位单位
12             sOutput += sCapital.substr(sAmount.substr(i, 1), 1) + sUnit.substr(i, 1);
13         } 
14         return sPrefix + sOutput.replace(/零角零分$/, ‘整‘).replace(/零[仟佰拾]/g, ‘零‘).replace(/零{2,}/g, ‘零‘)
15                                 .replace(/零([亿|万])/g, ‘$1‘).replace(/零+元/, ‘元‘).replace(/亿零{0,3}万/, ‘亿‘).replace(/^元/, "零元");
16     };

 

JS函数实现金额小写转大写,布布扣,bubuko.com

JS函数实现金额小写转大写

上一篇:一些好用的工具网站


下一篇:iOS开发真机调试报错