Javascript implements the method of adding a comma to each three digits, and JavaScript numbers are separated by three commas.
This example describes how to add a comma (,) to each three digits in JavaScript. Share it with you for your reference. The specific implementation method is as follows:
Function formatNum (str) {var newStr = ""; var count = 0; if (str. indexOf (". ") =-1) {for (var I = str. length-1; I> = 0; I --) {if (count % 3 = 0 & count! = 0) {newStr = str. charAt (I) + "," + newStr;} else {newStr = str. charAt (I) + newStr;} count ++;} str = newStr + ". 00 "; // automatically add two consoles after the decimal point. log (str)} else {for (var I = str. indexOf (". ")-1; I> = 0; I --) {if (count % 3 = 0 & count! = 0) {newStr = str. charAt (I) + "," + newStr;} else {newStr = str. charAt (I) + newStr; // concatenate characters one by one} count ++;} str = newStr + (str + "00 "). substr (str + "00 "). indexOf (". "), 3); console. log (str)} formatNum ('100. 13213. 24 '); // output 13,213.34 formatNum ('100. 000. 2 '); // output 132,134.20 formatNum ('20160301'); // output 132134 formatNum ('20160301. 236 '); // output 132,134.23
I hope this article will help you design javascript programs.