You are given an integer written as text, possibly with a leading - sign. Format it using the standard international digit grouping: starting from the rightmost digit, insert a comma every 3 digits, and keep the sign (if any) attached directly to the first digit group with no comma between the sign and the first digit.
Line 1: a string N — an optional leading -, followed by one or more digits with no leading zero (unless the value is exactly 0).
N with comma digit-grouping applied.
N ≤ 30N matches -?(0|[1-9][0-9]*)Example 1
Input
1234567
Expected
1,234,567
Explanation
Grouping from the right in 3s gives 1,234,567.
Example 2
Input
-42
Expected
-42
Explanation
Fewer than 4 digits need no comma; the sign is kept in front: -42.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →