Challenge
Given ASCII-encoded text input of any size, detect numbers between -1,000,000,000,000,000,000 and 1,000,000,000,000,000,000 exclusive and rewrite them with thousands separators. Use commas only and ignore leading zeroes. Leave the rest of the input unchanged.
Test cases
Input | Output | Description |
---|---|---|
123 |
123 |
|
1234 |
1,234 |
|
-12345 |
-12,345 |
|
x12345y |
x12,345y |
ignore surrounding characters |
1234 xyz 56789 |
1,234 xyz 56,789 |
|
1234,56789 |
1,234,56,789 |
1,234 and 56,789 are separate numbers |
0001234 |
0001,234 |
zeroes are not part of the number |
10000000 |
10,000,000 |
only leading zeroes are ignored |
1234567891234567890 |
1234567891234567890 |
1,234,567,891,234,567,890 is outside the range |
0123456789123456789 |
0123,456,789,123,456,789 |
Standard loopholes apply. Shortest code in bytes wins.
0001234
and1234567891234567890
as exceptions makes this a less attractive challenge. \$\endgroup\$123456789000123456789
->123456789000123456789
(solutions might conceivably parse it by mistake as123456789
plus000123456789
) \$\endgroup\$0123456789123456789
->0123,456,789,123,456,789
(18 digits if the leading zero is disgarded). Some entries fail on this one. \$\endgroup\$