Practice/Oracle/Add two decimal strings e.g. '123.45' + '236.7'
CodingOptional
You are given two non-negative numbers represented as strings. Each string may contain a decimal point followed by fractional digits. Your task is to add these two numbers and return the sum as a string.
The decimal points in the two input strings may appear at different positions, meaning the numbers can have different numbers of fractional digits. You must implement this addition without converting the strings to floating-point numbers (to avoid precision issues).
1 <= num1.length, num2.length <= 10^4num1 and num2 consist of digits and at most one decimal pointnum1 and num2 represent valid non-negative decimal numbersExample 1:
` Input: num1 = "123.45", num2 = "236.7" Output: "360.15" Explanation: We align the decimal points mentally: 123.45
360.15 `
Example 2:
Input: num1 = "999", num2 = "1" Output: "1000" Explanation: Both are integers, simple addition with carry propagation.
Example 3:
Input: num1 = "0.001", num2 = "0.999" Output: "1.0" Explanation: The sum is 1.000, but we keep one decimal place to show it was a decimal operation. Actually, trailing zeros should be removed, so "1.0" or "1" depending on implementation choice. For consistency, "1.0" is acceptable, but "1" is cleaner.