Fraction to recurring Decimal

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.