Results 1 to 13 of 13
  1. #1
    herpeslurpy is offline Member
    Join Date
    Nov 2012
    Posts
    40
    Rep Power
    0

    Post How to enclose certain digits of a value double in parentheses

    I need to make a program that converts fractions to decimal form, but cuts it off after a certain amount of digits, and then repeating decimals after that need to be closed in parentheses. I have most of it done, but the part I'm stumped with is how to enclose the repeating digits in parentheses (Strings are not my strong point

    here's my code so far:
    Java Code:
    import java.math.RoundingMode;
    import java.text.DecimalFormat;
    import java.util.Scanner;
    public class fractions {
    	public static void main(String[] args) {
    		double num = 0, denom  = 0;
    		double dec = 0;
    		Scanner numerator = new Scanner(System.in);
    		Scanner denominator = new Scanner(System.in);
    		System.out.println("Enter a fraction, each side no more than 2 digits: ");
    		num+=numerator.nextInt();
    		denom+=numerator.nextInt();
    		dec = num / denom;
    		double d = dec;
    		DecimalFormat df = new DecimalFormat("0.##");
    		df.setRoundingMode(RoundingMode.DOWN);
    		
    		double outputNum = Double.valueOf(df.format(d));
    		String outputString = df.format(d);
    		System.out.println("The decimal form is: " +outputString);
    	
    	}
    }
    Thanks in advance to anyone who can help me with this.

  2. #2
    Wnt2bsleepin is offline Senior Member
    Join Date
    Feb 2012
    Posts
    219
    Rep Power
    3

    Default Re: How to enclose certain digits of a value double in parentheses

    Just to clarify, you want the output to look something like this?

    Java Code:
    8.3333(333)

  3. #3
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Location
    Munich
    Posts
    297
    Rep Power
    3

    Default Re: How to enclose certain digits of a value double in parentheses

    How about this:
    - Declare a helper string
    - Look for the dot in the formatted number string
    - Copy all until that digit (the one you want to start with) in the helper
    - Append a opening bracket to helper
    - Copy the rest of the string to the end of the helper
    - Append a closing bracket
    Last edited by Sierra; 01-11-2013 at 02:21 PM.
    I like likes!

  4. #4
    herpeslurpy is offline Member
    Join Date
    Nov 2012
    Posts
    40
    Rep Power
    0

    Default Re: How to enclose certain digits of a value double in parentheses

    Quote Originally Posted by Wnt2bsleepin View Post
    Just to clarify, you want the output to look something like this?

    Java Code:
    8.3333(333)
    yes, once the decimals start repeating they need to be closed off; 5.143(55555555)

  5. #5
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Location
    Munich
    Posts
    297
    Rep Power
    3

    Default Re: How to enclose certain digits of a value double in parentheses

    How do you determine at which point they repeat? What is your criteria? e.g. for 6.88888889 ?
    I like likes!

  6. #6
    herpeslurpy is offline Member
    Join Date
    Nov 2012
    Posts
    40
    Rep Power
    0

    Default Re: How to enclose certain digits of a value double in parentheses

    Quote Originally Posted by Sierra View Post
    How do you determine at which point they repeat? What is your criteria? e.g. for 6.88888889 ?
    Here's a link to the actual problem, it can probably explain better than me:
    https://www.dropbox.com/s/ufej8xxhxr...2fractions.pdf

  7. #7
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Location
    Munich
    Posts
    297
    Rep Power
    3

    Default Re: How to enclose certain digits of a value double in parentheses

    Well I am not asking for the assignment but for your approach to the solution... ;)
    I like likes!

  8. #8
    herpeslurpy is offline Member
    Join Date
    Nov 2012
    Posts
    40
    Rep Power
    0

    Default Re: How to enclose certain digits of a value double in parentheses

    Quote Originally Posted by Sierra View Post
    Well I am not asking for the assignment but for your approach to the solution... ;)
    My bad, I should clarify; I was providing more details on the assignment so I could better explain it
    Last edited by herpeslurpy; 01-11-2013 at 04:21 PM.

  9. #9
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,015
    Blog Entries
    7
    Rep Power
    20

    Default Re: How to enclose certain digits of a value double in parentheses

    Maybe the following (very old) algorithm can be of help: it finds the repeating group of digits for a number 1/p where p is prime (not equal to 2 or 5). It has its origins in Vedic math (a very interesting topic).
    Without any proof:

    Java Code:
    public class RepFracs {
    	
    	public static void main(String[] args) {
    		
    		int prime= 7;
    		
    		int multiplier, factor;
    		
    		for (multiplier= 1; ; multiplier++)
    			if ((prime*multiplier)%10 == 9) {
    				factor= (prime*multiplier)/10;
    				if (++factor == 10) factor=1;
    				break;
    			}
    		
    		int length= 0, x=multiplier, carry= 0;
    		StringBuilder sb= new StringBuilder();
    		
    		do {
    			sb.append(""+x);
    			x*= factor; x+= carry;
    			carry= x/10; x= x%10;
    			length++;
    		}
    		while (x != multiplier || carry != 0);
    		
    		System.out.println(length+" "+sb.reverse());
    	}
    }
    kind regards,

    Jos
    Last edited by JosAH; 01-11-2013 at 05:07 PM.
    cenosillicaphobia: the fear for an empty beer glass

  10. #10
    herpeslurpy is offline Member
    Join Date
    Nov 2012
    Posts
    40
    Rep Power
    0

    Default Re: How to enclose certain digits of a value double in parentheses

    Quote Originally Posted by JosAH View Post
    Maybe the following (very old) algorithm can be of help: it finds the repeating group of digits for a number 1/p where p is prime (not equal to 2 or 5). It has its origins in Vedic math (a very interesting topic).
    Without any proof:

    Java Code:
    public class RepFracs {
    	
    	public static void main(String[] args) {
    		
    		int prime= 7;
    		
    		int multiplier, factor;
    		
    		for (multiplier= 1; ; multiplier++)
    			if ((prime*multiplier)%10 == 9) {
    				factor= (prime*multiplier)/10;
    				if (++factor == 10) factor=1;
    				break;
    			}
    		
    		int length= 0, x=multiplier, carry= 0;
    		StringBuilder sb= new StringBuilder();
    		
    		do {
    			sb.append(""+x);
    			x*= factor; x+= carry;
    			carry= x/10; x= x%10;
    			length++;
    		}
    		while (x != multiplier || carry != 0);
    		
    		System.out.println(length+" "+sb.reverse());
    	}
    }
    kind regards,

    Jos
    thanks! I can't test it out right now, I don't have my work with me, but I have a good feeling about this, and if doesn't work, thanks anyways.

  11. #11
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,015
    Blog Entries
    7
    Rep Power
    20

    Default Re: How to enclose certain digits of a value double in parentheses

    Quote Originally Posted by herpeslurpy View Post
    thanks! I can't test it out right now, I don't have my work with me, but I have a good feeling about this, and if doesn't work, thanks anyways.
    Well, it works for numbers 1/p (p is prime); it is one of the best tested algorithms because it's thousands years old ;-) Do with it what you want; I think it can be extended to 1/(p1*p2*p3 ... pn) ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  12. #12
    herpeslurpy is offline Member
    Join Date
    Nov 2012
    Posts
    40
    Rep Power
    0

    Default Re: How to enclose certain digits of a value double in parentheses

    Quote Originally Posted by JosAH View Post
    Well, it works for numbers 1/p (p is prime); it is one of the best tested algorithms because it's thousands years old ;-) Do with it what you want; I think it can be extended to 1/(p1*p2*p3 ... pn) ...

    kind regards,

    Jos
    thanks, but for some reason when I try to apply it, I'm getting an error with those If statements all the way to the end of the For loop, saying "unreachable code"

  13. #13
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,015
    Blog Entries
    7
    Rep Power
    20

    Default Re: How to enclose certain digits of a value double in parentheses

    Quote Originally Posted by herpeslurpy View Post
    thanks, but for some reason when I try to apply it, I'm getting an error with those If statements all the way to the end of the For loop, saying "unreachable code"
    You made typos then ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. what do double parentheses do? ()?
    By MW130 in forum New To Java
    Replies: 10
    Last Post: 01-06-2013, 09:30 PM
  2. It keeps trying to get me to close parentheses...
    By MrPosotive in forum New To Java
    Replies: 2
    Last Post: 09-24-2012, 07:54 AM
  3. Replies: 3
    Last Post: 10-27-2011, 05:23 PM
  4. need help with Number Pyramid with Double Digits
    By SmellyFoot in forum New To Java
    Replies: 5
    Last Post: 03-29-2011, 12:04 PM
  5. Replies: 1
    Last Post: 01-19-2011, 09:23 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •