This article describes the method of adding commas to every three digits of js. Share it 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 fill the two decimal points console.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; //Connect one by one} count++; } str = newStr + (str + "00").substr((str + "00").indexOf("."),3); console.log(str) }}formatNum('13213.24'); //Output 13,213.34formatNum('132134.2'); //Output 132,134.20formatNum('132134'); //Output 132,134.00formatNum('132134.236'); //Output 132,134.23I hope this article will be helpful to everyone's JavaScript programming.