Home  >  Article  >  Web Front-end  >  JS formatting numbers

JS formatting numbers

巴扎黑
巴扎黑Original
2016-12-19 14:17:091330browse

Recently, I have been involved in the processing of a lot of monetary values ​​when developing a mall. I will soon have to convert the data (for example: 12345-->1,2345...)

//Format the number fmoney("12345.675910", 3) , return 12,345.676

fmoneyFormatPoint : function(s, n) {  
        n = n > 0 && n <= 20 ? n : 2;  
        s = parseFloat((s + "").replace(/[^\d\.-]/g, "")).toFixed(n) + "";  
        var l = s.split(".")[0].split("").reverse(), r = s.split(".")[1];  
        t = "";  
        for (i = 0; i < l.length; i++) {  
            t += l[i] + ((i + 1) % 3 == 0 && (i + 1) != l.length ? "," : "");  
        }  
var amt = t.split("").reverse().join("") + "." + r;
        return amt;  //返回带有逗号的值
//return amt.replace(new RegExp(/,/g),&#39;&#39;);//返回没有带逗号的值
    }


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:js shake your phoneNext article:js shake your phone