09 Digit Commas

Digit commas formatting

Insert commas in the numbers for greater readability before each group of three digits.

Input text:

Ten Countries with the Highest Population:
1 China 1361220000
2 India 1236800000
3 United States 317121000
4 Indonesia 237641326
5 Brazil 201032714
6 Pakistan 184872000
7 Nigeria 173615000
8 Bangladesh 152518015
9 Russia 143600000
10 Japan 127290000

Desired output:

Ten Countries with the Highest Population:
1 China 1,361,220,000
2 India 1,236,800,000
3 United States 317,121,000
4 Indonesia 237,641,326
5 Brazil 201,032,714
6 Pakistan 184,872,000
7 Nigeria 173,615,000
8 Bangladesh 152,518,015
9 Russia 143,600,000
10 Japan 127,290,000

Honestly, this isn’t even that interesting for a parser. The important thing is that you be able to execute code before making the replacement. (This is Perl’s /e modifier to the regex. Examples in Perl.)

Original source.

Last modified March 15, 2024: Parsing unit. (015098c)