Format number into K and M for thousands and millions
JavaScript
function formatPrice(num) {
num = num.replace(/[^\d.-]/g, '');
num = parseInt(num);
if(num > 999 && num < 1000000){
return (num/1000).toFixed(1) + 'K';
}else if(num > 1000000){
return (num/1000000).toFixed(1) + 'M';
}else if(num < 1000){ return num; }
else { return num; }
}
var p1 = 'US $999.00';
var p2 = 'US $12,45,000.00';
console.log(formatPrice(p1));
console.log(formatPrice(p2));