Please implement a js script, which requires that the number be converted into kilobytes, for example, 10000 is converted to 10,000

Source: Internet
Author: User

Please implement a js script, which requires that the number be converted into kilobytes, for example, 10000 is converted to 10,000
Implement a js script to convert the number into a kilobytes

For example:
10000 ----> 10,000

10000121213 ----> 10,000,121,213

I accidentally saw this question today and thought about four solutions to share with you:

// Method 1 function parseNum (num) {var list = new String (num ). split (''). reverse (); for (var I = 0; I <list. length; I ++) {if (I % 4 = 3) {list. splice (I, 0, ',');} return list. reverse (). join ('');} console. log (parseNum (10000121213); // method 2 function parseNum (num) {var reg = /(? = (?! \ B) (\ d {3}) + $)/g; return String (num ). replace (reg, ',');} console. log (parseNum (10000121213); // method three strings. prototype. strReverse = function () {return this. split (''). reverse (). join ('');} function parseNum (num) {var str_num = String (num); var len = str_num.length; var tail = str_num.slice (0, len % 3 ); tail = tail. strReverse (); var reg =/\ d {3}/g; var list = str_num.strReverse (). match (reg); list. push (tail); var res = list. join (','). strReverse (); return res;} console. log (parseNum (10000121213); // Method 4 function parseNum (num) {var list = String (num ). split (''). reverse (); var temp = []; for (var I = 0, len = list. length; I <len; I = I + 3) {temp. push (list. slice (I, I + 3 ). join ('');} return temp. join (','). split (''). reverse (). join ('');} console. log (parseNum (10000121213 ));



Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.