call(back)
Algorithms & Data StructuresmediumSign in to star problems

Round Numeric Strings II

You are given a comma-separated string of numeric values, for example "2.5,-2.5,9.99,-0.4". The values may be far too large for built-in integer or float types in most languages, and converting to float would lose precision — so treat this as pure string manipulation.

Round every value to the nearest integer, rounding half away from zero, and return the rounded values comma-separated in the same order. Each result has no leading zeros and is never "-0". An empty string returns an empty string.

"2.5,-2.5,9.99,-0.4"   -> "3,-3,10,0"
"999.5"                -> "1000"
"123456789123456789123456789.5,-0.5" -> "123456789123456789123456790,-1"

Hints

Hint 1

Only the first fractional digit matters for direction: with half-away-from-zero, the magnitude rounds up exactly when that digit is 5 or more.

Hint 2

Round the magnitude and reattach the sign only when the result is not 0. Rounding up is big-integer addition: walk the integer digits right to left carrying a 1, and prepend a digit if the carry survives (999 to 1000).

Hint 3

The list itself is a split on commas, a map over the values, and a join.

Asked at