Format Number With Commas

revised code
function formatNumberWithCommas(number) {
var sum = number;

var tens=0;
var sumPlace = sum;

while ( sumPlace > 1 ) {
sumPlace = sumPlace / 10;
tens++;
}

// now we have the number of digits left of the decimal
var commaCount=0;
if ( tens > 1 ) {
tens = tens/3;
commaCount++;

}

// now we have the number of commas
var sumString = sum + "";
for (var i = 0; i < commaCount; i++) formattedSum = sumString.substr(0, sumString.length-3) + "," + sumString.substr(sumString.length-3); return formattedSum; }

0 Comments

You must be logged in to post a comment.