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.
6What's the expected output for
00000
? Or for10,0001
? – Arnauld – 2019-06-08T11:12:21.5474Another 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