Add comma to numbers every three digits Add comma to numbers every three digits jquery jquery

Add comma to numbers every three digits


@Paul Creasey had the simplest solution as the regex, but here it is as a simple jQuery plugin:

$.fn.digits = function(){     return this.each(function(){         $(this).text( $(this).text().replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,") );     })}

You could then use it like this:

$("span.numbers").digits();


You could use Number.toLocaleString():

var number = 1557564534;document.body.innerHTML = number.toLocaleString();// 1,557,564,534