Home > Web Front-end > JS Tutorial > body text

JS formatting numbers

巴扎黑
Release: 2016-12-19 14:17:09
Original
1329 people have browsed it

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;);//返回没有带逗号的值
    }
Copy after login


Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!