Insert commas into numbers

6

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.

Brandon

Posted 2019-06-08T10:44:00.537

Reputation: 119

Question was closed 2019-06-08T14:14:58.597

6What's the expected output for 00000? Or for 10,0001? – Arnauld – 2019-06-08T11:12:21.547

4Another ambiguous case: 1234.9876.5678 – Jonathan Allan – 2019-06-08T13:17:03.607

3I'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 – 2019-06-08T14:14:54.710

@Shaggy I have just edited the post with a lot more detailed information – Brandon – 2019-06-09T09:16:08.910

Answers

3

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+

Delete all leading zeros.

^\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.

Neil

Posted 2019-06-08T10:44:00.537

Reputation: 95 035

0000 returne 0,000. 10,0000 returns 10,0,00. – ouflak – 2019-06-08T12:07:19.693

I suggest deleting this and VTC as unclear. – Jonathan Allan – 2019-06-08T13:22:39.643

@Neil, I have just edited the post with a lot more detailed information on various cases. Thanks for your answer so far. – Brandon – 2019-06-09T09:17:04.897

@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 – 2019-06-09T10:18:21.097

@Neil any language is fine. Edit as much as possible or delete and start a new answer. – Brandon – 2019-06-09T10:21:35.163