JS添加千分位

JS添加千分位

第一段if可去

第二段if中num!==''解释:

0==''  -->  true

0==='' --> false

 1         //设置千分位
 2         function toThousands(num, decLength) {
 3             if (num == "0") {
 4                 return "";
 5             }
 6             if (num !== "" && num != null && num != undefined) {
 7                 num = String(num);
 8                 if (decLength > 0) {
 9                     num = parseFloat(num).toFixed(decLength);
10                 }
11                 else {
12                     num = parseInt(num);
13                 }
14                 var re = /\d{1,3}(?=(\d{3})+$)/g;
15                 var n = (num).toString().replace(/^(\d+)((\.\d+)?)$/, function (s, s1, s2) { return s1.replace(re, "$&,") + s2; });
16                 return n;
17             } else {
18                 return "";
19             }
20         }

 

posted on 2020-09-25 22:37  奥奥没有利  阅读(185)  评论(0)    收藏  举报