JavaScript implementation of the number of thousands of points in the way to summarize the "5 ways" _javascript skills

Source: Internet
Author: User

The examples in this article describe the way in which JavaScript implements thousands of decimal points. Share to everyone for your reference, specific as follows:

Although there are two months to go before the new year, the Battle of the Spring Festival robbery has been launched, the tragedy is that I have not grabbed the ticket, see a number of browsers, think of a classic interview question, yes, is the number of thousands of points. If you turn the number 87463297 to 87,463,297, there are many ways to do it, I only think of 5.

1, using the regular 0-width positive lookahead assertion (? =exp), the name is a bit difficult to remember, meaning that it asserts the position of its own appearance can match the expression of exp, the concept is not clear to this point can be poked here, not too much explanation. The character of the digital micrometer is that the number of digits after the first comma is a multiple of 3, Regular:/(\d{3}) +$/; the first comma can have up to 1 to 3 digits, and regular:/\d{1,3}/. Add up is the/\d{1,3} (\d{3}) +$/, the separator to add from the before, we will be the previous number "87" to replace the "87", why 87 is not 874? Since 874 only 5 digits behind, after 632 plus a separator, will only be 97, does not meet the requirements of the thousand, so the number of digits after the first delimiter must be a multiple of 3. To match the number 87, but also to ensure that the number of digits after 87 is a multiple of 3, and to replace the matching 87 to "87," you need to use (? =exp), here first define a variable var str = "87463297";

0 Wide Assertion
console.info (Str.replace (/\d{1,3} (? = (\d{3}) +$)/g,function (s) {return
  s+ ', '
})

2, the use of regular subkeys to replace, similar to the 1th method.

subkey
Console.info (Str.replace (\d{1,3}) (? = (\d{3}) +$)/g,function ($) {return
  $1=$1+ ', '
})

3, first the string into a group, using reverse inverted array after every 3 digits after the addition of a delimiter ",", except for the end of the string, and then back to the string.

Use string and array methods to
Console.info (Str.split (""). Reverse (). Join (""). Replace (/(\d{3}) +?/g,function (s) {return
  s+ ,";
}). Replace (/,$/, ""). Split (""). Reverse (). Join ("")

4, use while loop concatenation string every 3 digits plus a separator, the end of the

Use loop concatenation string every 3 plus one separator
var result= "",
  index = 0,
  len = str.length-1;
while (len>=0) {
  index%3===0&&index!==0 result+= "," +str[len]: Result+=str[len];
  len--;
  index++;
};
Result=result.split (""). Reverse (). Join ("");
Console.info (result);

5. Use while loop to push the separator in the array.

Using a while loop to push the separator in an array
var result= "",
  index = 0,
  len = str.length,
  i = len-1,
  arr = Str.split (""); C6/>while (len-index>0) {
  len>=index&&len-index!==len && arr.splice (len-index,0, ",");
  index+=3;
  i-=4;
};
Console.log (Arr.join (""));

Conclusion: The 1th method is the most concise, the performance is also the best, recommended use. Incidentally enclose all examples of demo source Download , if there are other better and more convenient way please tell me, thank you!

More readers interested in JavaScript-related content can view the site topics: "JavaScript Mathematical operational Usage Summary", "javascript json operation tips Summary", "JavaScript switching effects and techniques summary", " JavaScript Search Algorithm Tips Summary, "JavaScript animation effects and techniques Summary", "JavaScript Error and debugging skills Summary", "JavaScript data structure and algorithm skills summary" and "JavaScript traversal algorithm and Skills summary"

I hope this article will help you with JavaScript programming.

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.