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

• 01-11-2013, 05:34 AM
herpeslurpy
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:(shake):

here's my code so far:
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.
• 01-11-2013, 05:42 AM
Wnt2bsleepin
Re: How to enclose certain digits of a value double in parentheses
Just to clarify, you want the output to look something like this?

Code:

`8.3333(333)`
• 01-11-2013, 10:12 AM
Sierra
Re: How to enclose certain digits of a value double in parentheses
- 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
• 01-11-2013, 03:10 PM
herpeslurpy
Re: How to enclose certain digits of a value double in parentheses
Quote:

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

Code:

`8.3333(333)`

yes, once the decimals start repeating they need to be closed off; 5.143(55555555)
• 01-11-2013, 03:22 PM
Sierra
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 ?
• 01-11-2013, 03:39 PM
herpeslurpy
Re: How to enclose certain digits of a value double in parentheses
Quote:

Originally Posted by Sierra
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
• 01-11-2013, 04:57 PM
Sierra
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... ;)
• 01-11-2013, 05:15 PM
herpeslurpy
Re: How to enclose certain digits of a value double in parentheses
Quote:

Originally Posted by Sierra
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
• 01-11-2013, 05:59 PM
JosAH
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:

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
• 01-14-2013, 05:42 AM
herpeslurpy
Re: How to enclose certain digits of a value double in parentheses
Quote:

Originally Posted by JosAH
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:

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.
• 01-14-2013, 07:50 AM
JosAH
Re: How to enclose certain digits of a value double in parentheses
Quote:

Originally Posted by herpeslurpy
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
• 01-17-2013, 01:02 AM
herpeslurpy
Re: How to enclose certain digits of a value double in parentheses
Quote:

Originally Posted by JosAH
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"
• 01-17-2013, 08:25 AM
JosAH
Re: How to enclose certain digits of a value double in parentheses
Quote:

Originally Posted by herpeslurpy
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"