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

1. Member
Join Date
Nov 2012
Posts
40
Rep Power
0

## 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. Senior Member
Join Date
Feb 2012
Posts
219
Rep Power
9

## 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. AN21XX
Join Date
Mar 2012
Location
Munich
Posts
297
Rep Power
9

## 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
Last edited by Sierra; 01-11-2013 at 02:21 PM.

4. Member
Join Date
Nov 2012
Posts
40
Rep Power
0

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

Originally Posted by Wnt2bsleepin
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. AN21XX
Join Date
Mar 2012
Location
Munich
Posts
297
Rep Power
9

## 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 ?

6. Member
Join Date
Nov 2012
Posts
40
Rep Power
0

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

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

7. AN21XX
Join Date
Mar 2012
Location
Munich
Posts
297
Rep Power
9

## 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... ;)

8. Member
Join Date
Nov 2012
Posts
40
Rep Power
0

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

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
Last edited by herpeslurpy; 01-11-2013 at 04:21 PM.

9. ## 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.

10. Member
Join Date
Nov 2012
Posts
40
Rep Power
0

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

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:

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. ## Re: How to enclose certain digits of a value double in parentheses

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

12. Member
Join Date
Nov 2012
Posts
40
Rep Power
0

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

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"

13. ## Re: How to enclose certain digits of a value double in parentheses

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"
You made typos then ...

kind regards,

Jos

#### Posting Permissions

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