Fraction to Recurring Decimal
Given two integers numerator and denominator, return the fraction as a string in decimal form. If the fractional part is repeating, enclose the repeating portion in parentheses. Any valid answer under 10^4 characters is accepted.
Open official problem prompt ↗Produce the exact decimal representation of a fraction, explicitly marking any infinitely repeating tail.
Doing long division by hand: you keep bringing down zeros, and the moment you see a remainder you have seen before you know the digits will loop forever from that point.
- Input
- numerator = 1, denominator = 6
- Output
- "0.1(6)"
- Why
- 1/6 = 0.16666..., so the non-repeating digit 1 is followed by the repeating digit 6 in parentheses.
-2^31 <= numerator, denominator <= 2^31 - 1denominator != 0The answer string is guaranteed to be less than 10^4 characters