Search code examples
regexadobe-indesign

Formatting numbers to currency format in Indesign using GREP


I am trying to figure out how to use GREP to add comma (,) and Dollar sign ($) to numbers in InDesign.

For example,

  • A GREP configuration to turn 4 digit numbers such as 2000 into $2,000

  • A GREP configuration to turn 5 digits numbers such as 20000 into $20,000


Solution

    1. Firstly, open InDesign's Find/Change palette by either:

      • Choosing Edit > Find/Change from the menu bar.
      • Or, type +F on macOS, or type CTRL+F on Windows.
    2. Then, select the GREP panel in the Find/Change palette.


    TL;DR Use the solution provided in the Combining both Solutions A and B section below. However, please consider the solution provided in the Also change 6 digit numbers such as 100000 into $100,000 section below if that is something you want to do too.


    Solution A:

    A GREP configuration to turn 4 digit numbers such as 2000 into $2,000

    1. In the "Find What" area, enter the following pattern:

      (?<![0-9])([0-9]{1})([0-9]{3})(?![0-9]+?)
      

      A detailed explanation of this regexp pattern can be found here.

    2. In the "Change To" area enter the following:

      $$1,$2
      

    Solution B:

    A GREP configuration to turn 5 digits numbers such as 20000 into $20,000

    1. In the "Find What" area, enter the following pattern:

      (?<![0-9])([0-9]{2})([0-9]{3})(?![0-9]+?)
      

      A detailed explanation of this regexp pattern can be found here.

    2. In the "Change To" area enter the following:

      $$1,$2
      

    Combining both Solutions A and B:

    To achieve your requirement you can utilize the following single regexp pattern to perform a single "Find/Replace". This handles both requirements, i.e. it changes numbers such as 2000 into $2,000 and also handles changing numbers such as 20000 into $20,000

    1. In the "Find What" area, enter the following pattern:

      (?<![0-9])([0-9]{1,2}?)([0-9]{3})(?![0-9]+?)
      

      A detailed explanation of this regexp pattern can be found here.

    2. In the "Change To" area enter the following:

      $$1,$2
      

    Also change 6 digit numbers such as 100000 into $100,000

    If you also wanted to include changing 6 digit numbers such as 100000 into $100,000 then do the following:

    1. In the "Find What" area, enter the following pattern:

      (?<![0-9])([0-9]{1,3}?)([0-9]{3})(?![0-9]+?)
      

      A detailed explanation of this regexp pattern can be found here.

    2. In the "Change To" area enter the following:

      $$1,$2
      

    Additional Notes:

    1. The "Change To" text (i.e. $$1,$2) in the aforementioned solutions utilizes $1 and $2 to refer to the digits that were matched by the sub-expressions contained in the parenthesis (...) of the regexp pattern. The first dollar sign ($) and the comma (,) is inserted into the resultant text

    2. To perform the changes in your document you'll need to click the "Find" button followed by the "Change All" button.


    Edit:

    The author of the OP subsequently asked the following in the comments:

    also one last note, what would I add to the expression to make it search for 9 digit numbers?

    After performing the aforementioned solution provided in the Also change 6 digit numbers such as 100000 into $100,000 section consider running a second Find/Change using the following configuration:

    1. In the "Find What" area, enter the following pattern:

      (?<![0-9])([0-9]{1,3}?)([0-9]{3})([0-9]{3})(?![0-9]+?)
      

      A detailed explanation of this regexp pattern can be found here.

    2. In the "Change To" area enter the following:

      $$1,$2,$3
      

    So, after performing the two separate Find/Change tasks, i.e.

    1. The one provided in Also change 6 digit numbers such as 100000 into $100,000
    2. Followed by this new one provided in the Edit section.

    You'll handle all numbers/digits ranging from 4 to 9 digits in length.

    The placement of the comma's will be as shown in the table below:

    ┏━━━━━━━━┯━━━━━━━━━━━┯━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┯━━━━━━━━━━━┓
    ┃ No. of │ Example   │ Example      │                                                        │           ┃
    ┃ digits │ Before    │ After        │ Find What Pattern                                      │ Change To ┃
    ┣━━━━━━━━┿━━━━━━━━━━━┿━━━━━━━━━━━━━━┿━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┿━━━━━━━━━━━┫
    ┃   4    │ 1234      │ $1,234       │                                                        │           ┃
    ┠────────┼───────────┼──────────────┤                                                        │           ┃
    ┃   5    │ 12345     │ $12,345      │ (?<![0-9])([0-9]{1,3}?)([0-9]{3})(?![0-9]+?)           │ $$1,$2    ┃
    ┠────────┼───────────┼──────────────┤                                                        │           ┃
    ┃   6    │ 123456    │ $123,456     │                                                        │           ┃
    ┣━━━━━━━━┿━━━━━━━━━━━┿━━━━━━━━━━━━━━┿━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┿━━━━━━━━━━━┫
    ┃   7    │ 1234567   │ $1,234,567   │                                                        │           ┃
    ┠────────┼───────────┼──────────────┤                                                        │           ┃
    ┃   8    │ 12345678  │ $12,345,678  │ (?<![0-9])([0-9]{1,3}?)([0-9]{3})([0-9]{3})(?![0-9]+?) │ $$1,$2,$3 ┃
    ┠────────┼───────────┼──────────────┤                                                        │           ┃
    ┃   9    │ 123456789 │ $123,456,789 │                                                        │           ┃
    ┗━━━━━━━━┷━━━━━━━━━━━┷━━━━━━━━━━━━━━┷━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┷━━━━━━━━━━━┛