Given two integers representing the numerator and denominator of a fraction, return the fraction in string format.

If the fractional part is repeating, enclose the repeating part in parentheses.

If multiple answers are possible, return any of them.

It is guaranteed that the length of the answer string is less than 104 for all the given inputs.

&nbsp;
Example 1:

Input: numerator = 1, denominator = 2
Output: "0.5"


Example 2:

Input: numerator = 2, denominator = 1
Output: "2"


Example 3:

Input: numerator = 4, denominator = 333
Output: "0.(012)"


&nbsp;
Constraints:


	-231 &lt;=&nbsp;numerator, denominator &lt;= 231 - 1
	denominator != 0


from collections import defaultdict
class Solution:
    def fractionToDecimal(self, numerator: int, denominator: int) -> str:
        sign = "" if numerator*denominator >= 0 else "-"
        numerator, denominator = abs(numerator), abs(denominator)
        a = numerator//denominator
        numerator %= denominator
        if not numerator: return sign+str(a)
        fractions = []
        index = defaultdict(int)
        while 10*numerator not in index:
            numerator *= 10
            index[numerator] = len(fractions)
            fractions.append(str(numerator//denominator))
            numerator %= denominator
        i = index[10*numerator]
        return sign+str(a)+"."+"".join(fractions[:i])+"("+"".join(fractions[i:])+")" if numerator else sign+str(a)+"."+"".join(fractions[:i])
class Solution {
    public String fractionToDecimal(int numerator, int denominator) {
        if(numerator == 0){
            return  "0";
        }
        
        StringBuilder sb = new StringBuilder("");
        if(numerator<0 && denominator>0 || numerator>0 && denominator<0){
            sb.append("-");
        }
        
        long divisor = Math.abs((long)numerator);
        long dividend = Math.abs((long)denominator);
        long remainder = divisor % dividend;
        sb.append(divisor / dividend);
        
        if(remainder == 0){
            return sb.toString();
        }
        sb.append(".");
        HashMap<Long, Integer> map = new HashMap<Long, Integer>();
        while(remainder!=0){
            if(map.containsKey(remainder)){
                sb.insert(map.get(remainder), "(");
                sb.append(")");
                break;
            }
            map.put(remainder, sb.length());
            remainder*= 10;
            sb.append(remainder/dividend);
            remainder%= dividend;
        }
        return sb.toString();
    }
}
class Solution {
public:
    string fractionToDecimal(int numerator, int denominator) {
        unordered_map<int, int> umap;
        string result = "";
        if ((double) numerator / (double) denominator < 0) result.push_back('-');
        long long l_numerator = numerator > 0 ? numerator : -(long long) numerator;
        long long l_denominator = denominator > 0 ? denominator : -(long long) denominator;
        long long quotient = l_numerator / l_denominator;
        long long remainder = l_numerator % l_denominator;
        result.append(to_string(quotient));
        if (remainder == 0) return result;
        result.push_back('.');
        int position = result.size();
        umap[remainder] = position++;
        while (remainder != 0) {
            l_numerator = remainder * 10;
            quotient = l_numerator / l_denominator;
            remainder = l_numerator % l_denominator;
            char digit = '0' + quotient;
            result.push_back(digit);
            if (umap.find(remainder) != umap.end()) {
                result.insert(umap[remainder], 1, '(');
                result.push_back(')');
                return result;
            }
            umap[remainder] = position++;
        }
        return result;
    }
};
var fractionToDecimal = function(numerator, denominator) {
    if(numerator == 0) return '0'
    let result = ''
    if(numerator*denominator <0){
        result += '-'
    }
    
    let dividend = Math.abs(numerator)
    let divisor = Math.abs(denominator)
    result += Math.floor(dividend/divisor).toString()
    
    let remainder = dividend % divisor
    if(remainder == 0) return result
    
    result += '.'
    
    let map1 = new Map()
    while(remainder != 0){
        if(map1.has(remainder)){
            let i = map1.get(remainder)
            result = result.slice(0, i) + '(' + result.slice(i) + ')'
            break;
        }
        map1.set(remainder, result.length)
        remainder *= 10
        result += Math.floor(remainder/divisor).toString()
        remainder %= divisor
    }
    return result
};
Fraction to Recurring Decimal