Inserting Commas in Large Numbers Using GREP
Use this quick solution when you need to add commas to lots of numbers in InDesign.
Question: āI have an InDesign file with lots of tables with numbers. The client sent data/numbers without commas, but now they are asking that commas be included. Is there a GREP code to insert commas in numerals where needed?Ā ForĀ example, I need to update 25309 to 25,309 and 856194 to 856,194.ā
Answer: Yes, there is!Ā You can do it quite simply, using multiple rounds of Find/Replace with GREP. Hereās how it works:
Round 1: Insert the Thousands Comma
For the first pass, weāll insert the thousands comma in every number with four or more digits, so in Find What we want to match the last three digits of the number only if they are preceded by another digit (e.g., 1234 but not 123) and we want GREP to remember those three digits so we donāt lose them. The code for that is:
(?<=\d)(\d{3})$
$ matches āend of paragraph,ā
\d{3} matches 3 digits (we place them in parentheses in the full expression because we want InDesign to remember them for the next step)
(?<=\d) says āLook behind to see if thereās a digit there.ā
Copy and paste the code into the Find What field.
In the Change To field, copy and paste this code: ,$1
which means āinsert a comma, then the content we found and saved (those last three digits).”

Hereās the before and after, side by side, with a thousands comma inserted everywhere itās needed.
style=”font-size: revert; color: initial;”>Ā

Round 2, 3, 4, etc.
Now we want to look for 4 digits in a row followed by a comma (e.g., 1234,567) and insert another comma after the first one (1,234,567).
The Find What expression is: (\d)(\d{3},)
In this expression, (\d) matches any single digit and saves its value, and (\d{3},) matches three digits followed by a comma and saves the digits and comma. The whole expression will be matched only if there are four consecutive digits before a comma andĀ there is no comma between the first and second digits.
The Change To expression is: $1,$2
Like in Round 1,Ā $1 is the first saved match (first digit of four) and $2Ā is the second saved match (the last three digits plus the comma).

Run this as many times as you need to, until it canāt find any more.
Hereās the before and after of one round. Notice that thereās still a missing comma in that last line:

You just run the exact same Find/Change again to insert the ābillions comma,ā again to insert a ātrillions comma,ā and so on.
This article fromĀ CreativePro Magazine is for members only. To continue reading, please log in above, orĀ sign up for a membership today! Thanks for supporting CreativePro!
Commenting is easier and faster when you're logged in!
Recommended for you
Creative Cloud Storage: Where Does It Go?
When you create using Adobe Creative Cloud applications, where is your work save...
A Photoshop Script to Delete Empty Layers
As a Photoshop user, one of the kindest things you can do for anyone who has to...
Tip of the Week: Deleting Text With Find/Change
This tip was sent to Tip of the Week email subscribers on AugustĀ 7, 2014. Sign u...
