Skip to main content
Inspiring
June 10, 2014
Answered

Format long numbers with commas

  • June 10, 2014
  • 2 replies
  • 2435 views

Hi

I have a table with a lot of 4-digit+ numbers

I would like to add commas to them in the correct places, E.G:

     2000  should be  2,000

     23783  should be  23,783

and so on.

Is there a built in method to do this?

If not, what is the best way to accomplish this?

Thanks,

Davey

This topic has been closed for replies.
Correct answer Jump_Over

Hi,

Use find...replace with Grep:

find what: (\d+)(\d{3})

change to: $1,$2

and repeat it till each multinumber (7+) is matched

Jarek

2 replies

Inspiring
June 18, 2014

Hi, try this

grep {findWhat:"(\\d)(\\d\\d\\d)\\b"} {changeTo:"$1,$2"}

repeat n times

Jump_Over
Legend
June 10, 2014

23783  should be  23,783

What about 2,3783? It never happends?

Jarek

myDaveyAuthor
Inspiring
June 10, 2014

Hi Jarek

Thanks for replying

What about 2,3783? It never happends?

I am not sure what you mean... at least not where I am from!

Its always only 3 digits per comma

3,000

30,000

300,000

3,000,000

Community Expert
June 16, 2014

To make it cool, using Multi-Find/Change, save the regex and place 10 times in a MFC set. So, with 1 click, you treat all until:

@ Obi-wan: To make it really cool, write it in the way like Jeffrey Friedl did it: \d(?=(\d{3})+\b) Notice here the Plus-repeater after the digits group.

@ myDavey: This one works for me with .replace()

var string = "1000000";

var result = string.replace(/(\d)(?=(\d{3})+\b)/g, "$1,");

alert (result);

–Kai


@Kai – yes, really cool. Obi-wan is using the marker for "left dilimiter of a word" \> instead of \b which marks both, the right and the left delimiter of a word. Both are working fine here.This is working not only in a RegEx with a replace() function, but also with the GREP functionality in InDesign.

Searching for a single digit that is followed by a multitude of 3 digits delimited by a word boundary. The "followed-by" is expressed as a positive look ahead. Replace the found digit by itself adding a comma (or with a German text you would use a dot instead of the comma).

Instead of $1 one could also write $0.

Works the same in GREP land.

Not so with using replace() and a regular expression!

String = "1000000"

Result with GREP, using Kai's example string with $0 would be:

"1,000,000"

Result with replace() using $0 would be:

"$0,00$0,000"

It seems, that $0 is not working at all with replace().
It's just interpreted as a normal string.

Can someone verify this?

Uwe