---
# System prepended metadata

title: 0166. Fraction to Recurring Decimal
tags: [Leetcode, Medium, Math]

---

# 0166. Fraction to Recurring Decimal
###### tags: `Leetcode` `Medium` `Math`
Link: https://leetcode.com/problems/fraction-to-recurring-decimal/
## 思路
## Code
```java=
class Solution {
    public String fractionToDecimal(int numerator, int denominator) {
        if(numerator == 0) return "0";
        StringBuilder ans = new StringBuilder();
        if((numerator>0)^(denominator>0)) ans.append("-");
        long num = Math.abs((long)numerator);
        long den = Math.abs((long)denominator);
        
        //integral part
        ans.append(num/den);
        num %= den;
        if(num==0) return ans.toString();
        
        //fractional part
        ans.append(".");
        Map<Long, Integer> map = new HashMap<>();
        map.put(num, ans.length());
        while(num!=0){
            num*=10;
            ans.append(num/den);
            num %= den;
            if(map.containsKey(num)){
                int idx = map.get(num);
                ans.insert(idx, "(");
                ans.append(")");
                break;
            }
            else map.put(num, ans.length());
        }
        return ans.toString();
    }
}
```