Let's look at the examples directly:
The code copy is as follows:
function fmoney(s, n) //s: the float number passed, n: I hope to return the decimal point several digits
{
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
Called: fmoney("12345.675910", 3), return 12,345.676
The code copy is as follows:
function rmoney(s)
{
return parseFloat(s.replace(/[^/d/.-]/g, ""));
}
Return the number in the amount returned above to float.
The code copy is as follows:
rmoney(12,345.676) //Return result is: 12345.676