Home  >  Article  >  Web Front-end  >  Use js to format the amount to set the number of decimal places to retain_javascript skills

Use js to format the amount to set the number of decimal places to retain_javascript skills

WBOY
WBOYOriginal
2016-05-16 16:49:101301browse
Copy code The code is as follows:

//The formatting s of the amount is the parameter to be formatted (float point type), n is the number of digits left after the decimal point
function formatMoney(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;it =l[i] ((i 1)%3==0 && (i 1) != l.length ? "," : "");
}
return t.split(" ").reverse().join("") "." r;
}
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