JS formatted numeric instance code:
Numbers often need to be formatted, one is easy to read, and the actual needs, such as the amount of money is often separated by thousands of commas, the following is a function that can be this function, and can retain the decimal number of the specified bibliography, and automatically achieve rounding effect.
The code example is as follows:
function Fmoney (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 (var i =0;i<l.length;i++) { T+=l[i]+ ((i+1)%3==0&& (i+1)!=l.length? ",": ""); } return t.split (""). Reverse (). Join ("") + "." +R;} console.log (Fmoney (23163.1415926,3));
The above code implements the functions we need, the implementation process here is not much introduced, you can see the relevant reading.
Related reading:
The 1.parseFloat () function can be found in the Parsefloat () method section of JavaScript .
The 2.toFixed () function can refer to the toFixed () method section of the JavaScript number object.
The 3.split () function can refer to the section of the Split () method of the JavaScript string object .
The 4.reverse () function can refer to the reverse () method section of the JavaScript array object .
The 5.join () function can refer to the section of the join () method of the JavaScript array object .
JS formatted numeric instance code