Thousand separator regex

s/\d{1,3}(?=(\d{3})+(?!\d))/$&,/g

The amount of code that goes in to formatting numbers can be silly sometimes, especially when you realise it can be done as a regex.

Note that if the number has a DP longer than 2, it’ll format it – I suggest splitting out the DP first, and bash them back together.

(via)

2 Responses to “Thousand separator regex”

  1. [...] GeekTool output, you need to patch the source to interpret command output as utf-8. Original Post Sample Implementation [...]

  2. Ahhh, thanks for that. There are some truly awful code samples around for adding thousand separators to a number, this left me with a warm fuzzy feeling at the end of the day.

Leave a Reply
Not required

CODE: Please escape code and wrap in <pre><code>, doing so will automatically syntax highlight