JS formatted numeric instance code

Source: Internet
Author: User
Tags javascript array

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.