Use JavaScript to format numbers (three-Node Method)

Source: Internet
Author: User

Someone asks a question about digit formatting. For a string of numbers, implement the three-byte expression (for example, 629,200,000.206), and split the number from the back to the front, each three digits into segments, separate them with commas. It is required to use JavaScript to convert the number into an integer first, and then convert it into a three-node subscribe representation, and write a function.

Reference content is as follows:
Function format_number (n ){
Var B = parseInt (n). toString ();
Var len = B. length;
If (len <= 3) {return B ;}
Var r = len % 3;
Return r> 0? B. slice (0, r) + "," + B. slice (r, len ). match (/\ d {3}/g ). join (","): B. slice (r, len ). match (/\ d {3}/g ). join (",");
}

<Br/> <center> If the page cannot be displayed, press Ctrl + F5 to refresh the page. For more webpage code: <a href = 'HTTP: // www.bkjia.com/'title = "bkjia.com" target = '_ blank'> http://www.bkjia.com/</a> </center>

Tip: the code can be modified before running!

Related Article

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.