classSolution {publicStringfractionToDecimal(int numerator,int denominator) {StringBuilder result =newStringBuilder();String sign = (numerator <0== denominator <0|| numerator ==0) ?"":"-";long num =Math.abs((long) numerator);long den =Math.abs((long) denominator);result.append(sign);result.append(num / den);long rem = num % den;if (rem ==0)returnresult.toString();result.append(".");Map<Long,Integer> map =newHashMap(); // store numerator as repetition of same numerator will cause recurringwhile (rem !=0) {if (!map.containsKey(rem)) {map.put(rem,result.length()); // for a given numerator its (num*10)/den starts from this idx } else {int idx =map.get(rem);returnresult.substring(0, idx) +"("+result.substring(idx) +")"; } rem *=10;result.append(rem / den); rem = rem % den; }returnresult.toString(); }}