In Javascript I have a number and I want to add comma to it when it’s displayed as a string.
I can add comma to the number like this:
function numberWithCommas(value) {
return value.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}
But I don’t want to call a method on every number that we have to get this. I want to do something similar to this:
Number.prototype.toString = function(radix) {
return numberWithCommas(this);
}
So when I do the following, the right value will show up:
var num = 100000; alert(num); // 100,000
Can’t get the above to work. Any ideas?
What about creating a new number function?
http://jsfiddle.net/kyX8x/1/