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.