Javascript code to convert a number into a kilobytes

Source: Internet
Author: User
Tags join regular expression ticket

Even though there are still two months before the Chinese New Year's Eve, the battle for ticket snatching has started. The tragedy is that I have not yet obtained the ticket. When I saw a number on a browser, I thought of a classic interview question. That's right, that is, the number is switched to the sub-location. For example, if you convert the number 87463297 to 87,463,297, there are many methods. Here I only think of five.

1. Use the zero-width regular expression to predict the first assertions (? = Exp), the name is a bit hard to remember, which means that it can match the expression exp after the location where it appears. If you do not understand this concept, you can stamp it here. I will not explain it too much here. The number is a multiple of 3 after the first comma. Regular:/(\ d {3}) + $ /; A maximum of one to three numbers can be placed before the first comma. The regular expression is/\ d }/. Add up to/\ d {1, 3} (\ d {3}) + $/. To add the separator from the front to the back, replace the preceding number "87" with "87 ,", why is 87 not 874? Because there are only five digits after 874, after 632 is added with a separator, there will be only 97, which does not meet the requirements of the sub-location, so the number of digits after the first separator must be a multiple of 3. To match the number 87, make sure that the number of digits after the number 87 is a multiple of 3, and replace the matching 87 with "87,", you need to use (? = Exp). Here we first define a variable var str = "87463297 ";

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

2. Use the subitem of the regular expression to replace it, similar to the 1st method.

// Subitem console.info (str. replace (/(\ d {1, 3 })(? = (\ D {3}) + $)/g, function ($1) {return $1 = $1 + ','}))

3. Convert the string into an array first, and use reverse to reverse the array and then add a separator "," after each of the three digits, except at the end of the string, and then convert it back to the string.

// Use the string and array method console.info (str. split (""). reverse (). join (""). replace (/(\ d {3}) +? /G, function (s) {return s + ",";}). replace (/, $ /,""). split (""). reverse (). join (""))

4. Use the while loop to concatenate a character string and add a separator at every three digits.

// Use the loop concatenation string to add a separator var result = "", index = 0, len = str at every three. 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 the while loop to push the separator in the array.

// Use the while loop to push the separator var result = "", index = 0, len = str in the array. length, I = len-1, arr = str. split (""); while (len-index> 0) {len> = index & len-index! = Len & arr. splice (len-index, 0, ","); index + = 3; I-= 4 ;}; console. log (arr. join (""));

Conclusion: The 1st methods are the most concise, and the performance is the best. We recommend that you use them.

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.