6
\$\begingroup\$

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.

\$\endgroup\$

closed as unclear what you're asking by Jonathan Allan, HyperNeutrino, Arnauld, Shaggy, Nick Kennedy Jun 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.

  • 6
    \$\begingroup\$ What's the expected output for 00000? Or for 10,0001? \$\endgroup\$ – Arnauld Jun 8 at 11:12
  • 4
    \$\begingroup\$ Another ambiguous case: 1234.9876.5678 \$\endgroup\$ – Jonathan Allan Jun 8 at 13:17
  • 3
    \$\begingroup\$ 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. \$\endgroup\$ – Shaggy Jun 8 at 14:14
  • \$\begingroup\$ @Shaggy I have just edited the post with a lot more detailed information \$\endgroup\$ – Brandon Jun 9 at 9:16
3
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ 0000 returne 0,000. 10,0000 returns 10,0,00. \$\endgroup\$ – ouflak Jun 8 at 12:07
  • \$\begingroup\$ I suggest deleting this and VTC as unclear. \$\endgroup\$ – Jonathan Allan Jun 8 at 13:22
  • \$\begingroup\$ @Neil, I have just edited the post with a lot more detailed information on various cases. Thanks for your answer so far. \$\endgroup\$ – Brandon Jun 9 at 9:17
  • \$\begingroup\$ @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? \$\endgroup\$ – Neil Jun 9 at 10:18
  • \$\begingroup\$ @Neil any language is fine. Edit as much as possible or delete and start a new answer. \$\endgroup\$ – Brandon Jun 9 at 10:21

Not the answer you're looking for? Browse other questions tagged or ask your own question.