JS數字小寫轉換大寫

來源:互聯網
上載者:User
在一個文字框裡輸入一個數值,在另一個地方顯示的時候自動轉換成大寫,數值是一個錢數,保留兩位小數,例如100.00,壹佰元零角零分。
View Code

 1 <script type="text/javascript">
2 //主函數
3 function DX(n) {
4 if (!/^(0|[1-9]\d*)(\.\d+)?$/.test(n))
5 return "資料非法";
6 var unit = "千百拾億千百拾萬千百拾元角分", str = "";
7 n += "00";
8 var p = n.indexOf('.');
9 if (p >= 0)
10 n = n.substring(0, p) + n.substr(p+1, 2);
11 unit = unit.substr(unit.length - n.length);
12 for (var i=0; i < n.length; i++)
13 str += '零壹貳三肆伍陸柒捌玖'.charAt(n.charAt(i)) + unit.charAt(i);
14 return str.replace(/零(千|百|拾|角)/g, "零").replace(/(零)+/g, "零").replace(/零(萬|億|元)/g, "$1").replace(/(億)萬|壹(拾)/g, "$1$2").replace(/^元零?|零分/g, "").replace(/元$/g, "元整");
15 }
16 //以下為測試輸出
17 alert('1526365.56 '+DX(1526365.56))
18 </script>
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.