Problem · Hash Table
Fraction to Recurring Decimal
Learn this problemProblem statement
Given signed integers numerator and nonzero denominator, return their exact decimal representation.
If the fractional part repeats, enclose the shortest repeating suffix in parentheses. Do not include unnecessary trailing zeroes.
Function
fractionToDecimal(numerator: int, denominator: int) → StringExamples
Example 1
numerator = 1denominator = 2return = "0.5"The decimal terminates.
Example 2
numerator = 4denominator = 333return = "0.(012)"The remainder repeats after the digits 012.
Constraints
-2^31 <= numerator <= 2^31 - 1-2^31 <= denominator <= 2^31 - 1denominator != 0