There is a random number (not necessarily positive or integer), say, -14383040.327843. I want to turn it into "(14,383,040)". So to spell it:
- negative number has to be in brackets
- fraction part should be stripped off without rounding
- every N decimal places (3 by default) from right to left there has to be a separator (
,by default) - no leading zeros
What is the fastest (across major browsers) way to get such string?
value < 0 ? '(' + String(value.toFixed(0)).split('').reverse().chunk(3).map(function(chunk) { return chunk.reverse().join(''); }).join(',') + ')' : String(value.toFixed(0)).split('').reverse().chunk(3).map(function(chunk) { return chunk.reverse().join(''); }).join(',')"-231423421342134.327843".split(".")[0].replace(/\B(?=(\d{3})+(?!\d))/g, ",").replace(/^-(.+)/,"($1)");