Note that there are many places to deal with long and int
such as Assignment N, d to determine if it should be a negative number.
If written
long n = (long) numerator>0? Numerator:-numerator; long D = (long) denominator>0? Denominator:-denominator;
There will be an error, possibly a numerator as int too short, so the front plus minus sign error
Public classSolution { PublicString Fractiontodecimal (intNumerator,intdenominator) { //I find it particularly clear to benefit from a solution to https://leetcode.com/discuss/27159/my-java-solution//Long n = math.abs (numerator), d = math.abs (denominator);//it could be a particularly large number. Longn =numerator; LongD =denominator; N= n>0? n:-N; D= d>0? d:-D; //don't forget to change all positive numbers.String S= Long.tostring (n/d); intindex = 0; HashMap<long, integer> HM =NewHashmap<long, integer>(); N= n%D; if(n!=0) s+= "."; Index+ = S.length ();//Current Digit ' s position,//The idx is placed here in time to reflect the position after the decimal point//multiply by 10 to calculate the number of digits after the decimal point while(n>0){ if(Hm.get (n) = =NULL) hm.put (n, index); Else{s= S.substring (0,hm.get (n)) + "(" + s.substring (Hm.get (n), index) + ")"; Break; } N*=10; S+ = Long.tostring (n/d); N%=D; Index++; } //if (numerator*denominator<0) s = "-" +s;//it could be a particularly large number. if((numerator<0&&denominator>0) | | (numerator>0&& denominator<0)) s = "-" +s; returns; }}
Fraction to recurring Decimal