Several Methods for adding a kilobytes of characters to formatting numbers in node. js _ node. js-js tutorial

Source: Internet
Author: User
This article mainly introduces node. in js, You Can format numbers to add thousands of characters. This article provides three implementation methods and provides the instance Code respectively. For more information, see the processing method separated by commas (,).

Regular Expression mode:

The Code is as follows:


"15000000 ". split (""). reverse (). join (""). replace (/(\ d {3})/g, "$1 ,"). split (""). reverse (). join ("");

"115000000". split (""). reverse (). join (""). replace (/(\ d {3 })(? = [^ $])/G, "$1,"). split (""). reverse (). join ("");

Var str = '1970. 100'. replace (/(? = (?! ^ )(? : \ D {3}) + (? : \. | $) (\ D {3} (\. \ d + $ )?) /G, ', $1 ');
Console. log (str)

'123'. replace (/\ B (? = (\ D {3}) + $)/g ,',')

Loop Search Method

The Code is as follows:


Function formatNumber (value ){
Value = value. toString ();
If (value. length <= 3 ){
Return value;
} Else {
Return formatNumber (value. substr (0, value. length-3) + ',' + value. substr (value. length-3 );
}
}

ToLocaleString function:

The Code is as follows:


15000000 .. toLocaleString ();

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.