Home  >  Article  >  Web Front-end  >  js amount formatting and round-trip conversion example_javascript skills

js amount formatting and round-trip conversion example_javascript skills

WBOY
WBOYOriginal
2016-05-16 16:58:301249browse

Let’s look at the example directly:

Copy the code The code is as follows:

function fmoney(s, n) //s: the float number passed in, n: the number of decimal points you want to return
{
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 ? "," : "");
}
return t .split("").reverse().join("") "." r;
}

The function of this function is to

call: fmoney(" 12345.675910", 3), returns 12,345.676
Copy code The code is as follows:

function rmoney(s )
{
return parseFloat(s.replace(/[^d.-]/g, ""));
}

Format the amount returned above The number is returned as float type.
Copy code The code is as follows:

rmoney(12,345.676) //The return result is: 12345.676
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