You are not allowed to parse the input into a built-in floating type (to avoid overflow and precision issues). Work directly on strings.
round(s) from scratchGiven a string s representing a decimal number, return a string representing the value rounded to the nearest integer.
s
may include an optional sign (
+
/
-
) and an optional decimal point.
"-.2"
(equivalent to
-0.2
)
"2."
(equivalent to
2.0
)
float()
Clarify and implement a deterministic tie-breaking rule (e.g., exactly .5 cases).
s to a given precision pGiven two strings:
s
: a decimal number as a string
p
: a positive decimal precision as a string
Return s rounded to the nearest multiple of p (as a string).
Examples:
s = "12567"
,
p = "100"
→ return
"12600"
s = "1234.678"
,
p = "0.1"
→ return
"1234.7"
Assume p is a power of 10 (e.g., 1000, 0.01, 0.1). Specify how you format the output (e.g., whether to keep trailing zeros).