码迷,mamicode.com
首页 > Web开发 > 详细

js 金额小写转换为大写

时间:2014-12-26 16:36:26      阅读:161      评论:0      收藏:0      [点我收藏+]

标签:

<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>

 

js 金额小写转换为大写

标签:

原文地址:http://www.cnblogs.com/kennyliu/p/4186859.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!