<p>Given two integers representing the <code>numerator</code> and <code>denominator</code> of a fraction, return <em>the fraction in string format</em>.</p>

<p>If the fractional part is repeating, enclose the repeating part in parentheses.</p>

<p>If multiple answers are possible, return <strong>any of them</strong>.</p>

<p>It is <strong>guaranteed</strong> that the length of the answer string is less than <code>10<sup>4</sup></code> for all the given inputs.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> numerator = 1, denominator = 2
<strong>Output:</strong> &quot;0.5&quot;
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> numerator = 2, denominator = 1
<strong>Output:</strong> &quot;2&quot;
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> numerator = 4, denominator = 333
<strong>Output:</strong> &quot;0.(012)&quot;
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>-2<sup>31</sup> &lt;=&nbsp;numerator, denominator &lt;= 2<sup>31</sup> - 1</code></li>
	<li><code>denominator != 0</code></li>
</ul>

class Solution:
    def fractionToDecimal(self, numerator: int, denominator: int) -> str:

        # case when the numerator is zero, the result is simply "0"
        if numerator == 0:
            return "0"
        
        # list to build the result string, starting with the sign and integer part
        result = []
        
        # handle the sign of the result:
        # append a minus sign if either numerator or denominator is negative, but not both
        if (numerator < 0) ^ (denominator < 0):
            result.append("-")
        
        # convert both numerator and denominator to absolute values for simplicity
        numerator = abs(numerator)
        denominator = abs(denominator)
        
        # compute the integer part of the division and append it to the result
        integer_part = numerator // denominator
        result.append(str(integer_part))
        
        # calculate the remainder of the division to determine if there is a fractional part
        remainder = numerator % denominator
        # if there's no remainder, return the result (just the integer part)
        if remainder == 0:
            return "".join(result)
        
        # otherwise, we have a fractional part, so we append the decimal point
        result.append(".")
        
        # dictionary to store the remainder and its corresponding index in the result list
        # this helps detect repeating remainders
        remainder_dict = {}
        
        # long division to compute the decimal part:
        # we continue dividing as long as the remainder is non-zero
        while remainder != 0:

            # print()

            # print(remainder)

            # if the remainder has been seen before, this means the decimal part is repeating
            if remainder in remainder_dict:
                # insert the opening parenthesis at the index where this remainder first appeared
                result.insert(remainder_dict[remainder], "(")
                # append the closing parenthesis at the end
                result.append(")")
                break  # repeating part found, so we can stop here
            
            # store the current remainder with the index where its corresponding digit will appear
            remainder_dict[remainder] = len(result)
            
            # multiply the remainder by 10 to simulate the next digit in long division
            remainder *= 10
            
            # print(remainder)

            # get the next digit by performing integer division with the denominator
            decimal_part = remainder // denominator

            # print(decimal_part, remainder, denominator)

            # append this digit to the result
            result.append(str(decimal_part))
            
            # update the remainder (mod operation) for the next iteration
            remainder %= denominator
        
        # join the result list into a string and return it
        return "".join(result)


16800