js格式化數字 金額按千位逗號分隔


 1 // 返回數字
 2 function removeFormatMoney(s) {
 3     return parseFloat(s.replace(/[^\d\.-]/g, ""));
 4 }
 5 
 6 /* 
 7  * formatMoney(s,type) 
 8  * 功能:金額按千位逗號分隔
 9  * 參數:s,需要格式化的金額數值. 
10  * 參數:type,判斷格式化后的金額是否需要小數位. 
11  * 返回:返回格式化后的數值字符串. 
12  */ 
13 function formatMoney(s, type) {
14     if (/[^0-9\.]/.test(s))
15         return "0.00";
16     if (s == null || s == "null" || s == "")
17         return "0.00";
18     s = s.toString().replace(/^(\d*)$/, "$1.");
19     s = (s + "00").replace(/(\d*\.\d\d)\d*/, "$1");
20     s = s.replace(".", ",");
21     var re = /(\d)(\d{3},)/;
22     while (re.test(s))
23         s = s.replace(re, "$1,$2");
24     s = s.replace(/,(\d\d)$/, ".$1");
25     if (type == 0) {
26         var a = s.split(".");
27         if (a[1] == "00") {
28             s = a[0];
29         }
30     }
31     return s;
32 }

轉載:http://blog.csdn.net/evangel_z/article/details/12839657


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM