Tuesday, April 21, 2015

js / stackoverflow quickie: number with commas

This came up for me twice recently, so I'm archiving this stackoverflow solution for pretty-printing a number with commas:
function numberWithCommas(x) {
    return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
I know ideally it would check what the local separator character is, but this is ok for some hacks.

No comments:

Post a Comment