当前位置: 代码迷 >> Web前端 >> 数目字转大写字母
  详细解决方案

数目字转大写字母

热度:160   发布时间:2012-11-25 11:44:31.0
数字转大写字母
<script language="JavaScript">  
function DX(n) { //金额大写转换函数
            if (!/^(0|[1-9]\d*)(\.\d+)?$/.test(n))
                return "数据非法";
            var unit = "千百拾亿千百拾万千百拾元角分", str = "";
            n += "00";
            var p = n.indexOf('.');
            if (p >= 0)
                n = n.substring(0, p) + n.substr(p + 1, 2);
            unit = unit.substr(unit.length - n.length);
            for (var i = 0; i < n.length; i++)
                str += '零壹贰叁肆伍陆柒捌玖'.charAt(n.charAt(i)) + unit.charAt(i);

            document.getElementById("span_DX").innerText = str.replace(/零(千|百|拾|角)/g, "零").replace(/(零)+/g, "零").replace(/零(万|亿|元)/g, "$1").replace(/(亿)万|壹(拾)/g, "$1$2").replace(/^元零?|零分/g, "").replace(/元$/g, "元整");
        }
       
    </script>

 <span style="float: left;">代收货款:</span><span style="display: block; float: left;
                                    width: 20px; text-align: left;">¥</span><span style="float: left;"><input onkeyup="this.value=this.value.replace(/[^\d]/g,'')"
                                        onblur="DX(this.value)" size="10" class="TXT" />元</span>
<br />
<div>
<span id="span_DX" style="text-align: right; letter-spacing: 20px;"></span>
</div>