js 金额小写转换为大写

时间:2022-11-19 09:11:49
<script>
     jQuery(document).ready(function () {
         //当金额文本框失去焦点时,自动将数字转化为大写填充到 大写的文本框中
         $("#Amount").blur(function () {
             var amount = $("#Amount").val();
             amount = AmountLtoU(amount);
             $("#CapitalAmount").val(amount);
         });
     });

     function AmountLtoU(num) {
         ///<summery>小写金额转化大写金额</summery>
         ///<param name=num type=number>金额</param>
         if (isNaN(num)) return "";
         var strPrefix = "";
         if (num < 0) strPrefix = "(负)";
         num = Math.abs(num);
         if (num >= 1000000000000) return "无效数值!";
         var strOutput = "";
         var strUnit = '仟佰拾亿仟佰拾万仟佰拾元角分';
         var strCapDgt = '零壹贰叁肆伍陆柒捌玖';
         num += "00";
         var intPos = num.indexOf('.');
         if (intPos >= 0) {
             num = num.substring(0, intPos) + num.substr(intPos + 1, 2);
         }
         strUnit = strUnit.substr(strUnit.length - num.length);
         for (var i = 0; i < num.length; i++) {
             strOutput += strCapDgt.substr(num.substr(i, 1), 1) + strUnit.substr(i, 1);
         }
         return strPrefix + strOutput.replace(/零角零分$/, '整').replace(/零[仟佰拾]/g, '零').replace(/零{2,}/g, '零').replace(/零([亿|万])/g, '$1').replace(/零+元/, '元').replace(/亿零{0,3}万/, '亿').replace(/^元/, "零元");
     };
    </script>