---
# System prepended metadata

title: Leetcode 166
tags: [LeetCode]

---

# Leetcode 166
###### tags: `LeetCode`
**Solution** 
Runtime: 3 ms, faster than  9.01%
Memory Usage: 35.5 MB, less than  90.48%

    public static String fractionToDecimal(int numerator, int denominator) {
		long numeratorL = numerator;
		long denominatorL = denominator;
		StringBuilder sb = new StringBuilder();
		long remainder = numeratorL % denominatorL;
		if(numerator < 0 && denominator > 0 || numerator > 0 && denominator < 0 ) {
			sb.append("-");
		}
		numeratorL = Math.abs(numeratorL);
		denominatorL = Math.abs(denominatorL);
		remainder = numeratorL % denominatorL;
		sb.append(numeratorL / denominatorL);
		if(remainder == 0) return sb.toString();
		sb.append(".");
		List<Long> list = new ArrayList<Long>();
		list.add(remainder);
		String temp = "";
		long t = System.currentTimeMillis();
		while(true) {
			long idx = (remainder * 10) % denominatorL;
			if(list.indexOf(idx) != -1) {
				if(list.indexOf(idx) == 0) {
					temp += (remainder * 10) / denominatorL;
					sb.append("(");
					sb.append(temp);
					sb.append(")");
				}else if(idx == 0) {
					sb.append(temp);
				}else {
					temp += (remainder * 10) / denominatorL;
					sb.append(temp.substring(0,list.indexOf(idx)));
					sb.append("(");
					sb.append(temp.substring(list.indexOf(idx)));
					sb.append(")");
				}
				break;	
			}
			temp += (remainder * 10) / denominatorL;
			remainder = idx;
			list.add(idx);
		}
		System.out.println("t:"+(System.currentTimeMillis()-t));
		return sb.toString();
	}
