jsは数字を書式設定します.金額は千単位のコンマで区切られています.負号は括弧で区切られています.

5080 ワード

 1 //     
 2 function removeFormatMoney(s) {
 3     s = s.toString().replace("(","-").replace(")","");
 4     return parseFloat(s.replace(/[^\d\.-]/g, ""));
 5 }
 6 /* 
 7  * formatMoney(s,type) 
 8  *   :         ,     
 9  *   :s,          . 
10  *   :type,                . 
11  *   :            . 
12  */ 
13 function formatMoney(s, type) {
14     var result = s;
15     if (s < 0)
16         s = 0 - s;
17     if (/[^0-9\.]/.test(s))
18         return "0.00";
19     if (s == null || s == "null" || s == "")
20         return "0.00";
21     if (type > 0)
22         s = new Number(s).toFixed(type);
23     s = s.toString().replace(/^(\d*)$/, "$1.");
24     s = (s + "00").replace(/(\d*\.\d\d)\d*/, "$1");
25     s = s.replace(".", ",");
26     var re = /(\d)(\d{3},)/;
27     while (re.test(s))
28         s = s.replace(re, "$1,$2");
29     s = s.replace(/,(\d\d)$/, ".$1");
30     if (type == 0) {
31         var a = s.split(".");
32         if (a[1] == "00") {
33             s = a[0];
34         }
35     }
36     if (result < 0)
37         result = "(" + s + ")";
38     else
39         result = s;
40     return result;
41 }