hhfa1990
6/12/2015 - 7:11 PM

separar por comas cifra de miles

separar por comas cifra de miles

function formatNumber (num) {
    var decimales = num % 1;
	if(decimales == 0)
		var x = Math.round(num); //Si no tiene decimales, le quita los 0s
	else
		var x = num.toFixed(2); //REDONDEA A  DOS DECIMALES

    return x.toString().replace(/(\d)(?=(\d{3})+(?!\d))/g, "$1,")
}

console.info(formatNumber(1240.5));    // 1,240.5
console.info(formatNumber(1000240.5)); // 1,000,240.5