# Insert commas into numbers [closed]

Write a program that reads a string, and inserts commas to divide large numbers in groups of 3. i.e. 10000 => 10,000. It should 'fix' incorrectly formatted numbers as described below.

• It must work for all numbers on the same line
• It should not modify numbers after a decimal point. i.e. 1234.5678 => 1,234.5678
• Leading zeros should be removed (or inserted where appropriate):
• 00000 => 0
• 00001234 => 1,234
• .1234 => 0.1234
• 00.1234 => 0.1234
• 1000.1234 => 1,000.1234
• All non-digit characters should not be altered.
• It should correct, incorrectly placed "," characters only when followed before or after a digit:
• 10,,00 => 1,000
• 00,000 => 0
• a,2 => a2
• 2,a => 2a
• ,a2 => ,a2
• 0,a2 => 0a2
• ,,, => ,,,
• ,,,. => ,,,.
• ,,,1 => 1
• ,.,,1 => 0.1
• Numbers that contain multiple "." should not be treated as a number, and therefore no comma separation should be used:
• 1..3 => 1..3
• 1234.5678.9012 => 1234.5678.9012
• 1234,,.5678abc.123 => 1,234.5678abc0.123
• 1234.,.5678abc.123 => 1234.,.5678abc0.123
• Explanations to unusual cases:
• ,,,. => ,,,. (not a number, no special meanings to ",", ".")
• ,,,.0 => 0 (these characters are neighbouring a number and treated as such)
• 1,,, => 1
• ,,,1 => 1
• ,.,,1 => 0.1 (the "." indicates decimal number, remove incorrectly placed "," in number).
• ,.,,.1 => ,.,,.1 (not a number because multiple ".")
• a,,,b1234 = > a,,,b1,234 (first few characters are not neighbouring a number and treated as text)

Example inputs:

10000
10,000
$1234.12 ~$123456789.12345~
HELLO123456.99BYE
The_amount_€45678_is_$USD51805.84 5000000-1999999=3000001 !1234567.1234+1234567.1234=2469134.2468! 00000a00 0b0001234 .a.1234 00.1234 1000.1234 10,,00 00,000 a,2 2,a ,a2 0,a2 ,,, ,,,. ,,,1 ,.,,1 1..3 1234.5678.9012 1234,,.5678abc.123 1234.,.5678abc.123 ,,,. ,,,.0 1,,, ,,,1 ,.,,1 ,.,,.1 a,,,b1234  Corresponding outputs: 10,000 10,000$1,234.12
~$123,456,789.12345~ HELLO123,456.99BYE The_amount_€45,678_is_$USD51,805.84
5,000,000-1,999,999=3,000,001
!1,234,567.1234+1,234,567.1234=2,469,134.2468!
0a0
0b1,234
.a0.1234
0.1234
1,000.1234
1,000
0
a2
2a
,a2
0a2
,,,
,,,.
1
0.1
1..3
1234.5678.9012
1,234.5678abc0.123
1234.,.5678abc0.123
,,,.
0
1
1
0.1
,.,,.1
a,,,b1,234


Code golf: shortest code wins.

## closed as unclear what you're asking by Jonathan Allan, HyperNeutrino, Arnauld, Shaggy, Nick KennedyJun 8 at 14:14

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

• What's the expected output for 00000? Or for 10,0001? – Arnauld Jun 8 at 11:12
• Another ambiguous case: 1234.9876.5678 – Jonathan Allan Jun 8 at 13:17
• I've VTCed as unclear until the test cases in the comments have been addressed. Please ping me once they have so I can retract my vote or cast my vote to reopen. – Shaggy Jun 8 at 14:14
• @Shaggy I have just edited the post with a lot more detailed information – Brandon Jun 9 at 9:16

# Retina, 71 bytes

/[\d.,]+/_/\d/&/\..*\./^&(,

^0+

^\B
0
'.&\.?0+$/^\d+/_r\B... ,$&


Try it online! Link includes test cases. Explanation:

/[\d.,]+/_


Find all runs of digits, .s and ,s.

/\d/&


Ignore those with no digits.

/\..*\./^&


Ignore those with two .s.

(


Process the rest of the script on each remaining match.

,


Delete all commas.

^0+


^\B
0


Insert a leading zero if the number does not begin with a digit.

'.&


If the number contains a ....

\.?0+$ ... delete trailing zeros and any preceding .. /^\d+/_  On the part of the number before the .... r\B... ,$&


Match groups of three starting from the end, but leave at least one digit at the beginning, and insert commas before each match.

• 0000 returne 0,000. 10,0000 returns 10,0,00. – ouflak Jun 8 at 12:07
• I suggest deleting this and VTC as unclear. – Jonathan Allan Jun 8 at 13:22
• @Neil, I have just edited the post with a lot more detailed information on various cases. Thanks for your answer so far. – Brandon Jun 9 at 9:17
• @Brandon I don't think I can update my Retina 0.8.2 answer for the new requirements. Would you mind if I tried using Retina 1 instead? – Neil Jun 9 at 10:18
• @Neil any language is fine. Edit as much as possible or delete and start a new answer. – Brandon Jun 9 at 10:21