Results 1 to 13 of 13
 01112013, 04:34 AM #1Member
 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); } }
 01112013, 04:42 AM #2Senior Member
 Join Date
 Feb 2012
 Posts
 219
 Rep Power
 3
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)
 01112013, 09:12 AM #3AN21XX
 Join Date
 Mar 2012
 Location
 Munich
 Posts
 297
 Rep Power
 3
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 bracketLast edited by Sierra; 01112013 at 02:21 PM.
I like likes!
 01112013, 02:10 PM #4Member
 Join Date
 Nov 2012
 Posts
 40
 Rep Power
 0
 01112013, 02:22 PM #5AN21XX
 Join Date
 Mar 2012
 Location
 Munich
 Posts
 297
 Rep Power
 3
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!
 01112013, 02:39 PM #6Member
 Join Date
 Nov 2012
 Posts
 40
 Rep Power
 0
Re: How to enclose certain digits of a value double in parentheses
Here's a link to the actual problem, it can probably explain better than me:
https://www.dropbox.com/s/ufej8xxhxr...2fractions.pdf
 01112013, 03:57 PM #7AN21XX
 Join Date
 Mar 2012
 Location
 Munich
 Posts
 297
 Rep Power
 3
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!
 01112013, 04:15 PM #8Member
 Join Date
 Nov 2012
 Posts
 40
 Rep Power
 0
 01112013, 04:59 PM #9
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,309
 Blog Entries
 7
 Rep Power
 20
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()); } }
JosLast edited by JosAH; 01112013 at 05:07 PM.
cenosillicaphobia: the fear for an empty beer glass
 01142013, 04:42 AM #10Member
 Join Date
 Nov 2012
 Posts
 40
 Rep Power
 0
 01142013, 06:50 AM #11
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,309
 Blog Entries
 7
 Rep Power
 20
Re: How to enclose certain digits of a value double in parentheses
cenosillicaphobia: the fear for an empty beer glass
 01172013, 12:02 AM #12Member
 Join Date
 Nov 2012
 Posts
 40
 Rep Power
 0
 01172013, 07:25 AM #13
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,309
 Blog Entries
 7
 Rep Power
 20
Similar Threads

what do double parentheses do? ()?
By MW130 in forum New To JavaReplies: 10Last Post: 01062013, 09:30 PM 
It keeps trying to get me to close parentheses...
By MrPosotive in forum New To JavaReplies: 2Last Post: 09242012, 07:54 AM 
Java help 25element array of digits to store integers as large as 25 digits
By CCC3 in forum New To JavaReplies: 3Last Post: 10272011, 05:23 PM 
need help with Number Pyramid with Double Digits
By SmellyFoot in forum New To JavaReplies: 5Last Post: 03292011, 12:04 PM 
Value should be 7 or 8 digits .If 8 digits, the last should be a character
By renu in forum New To JavaReplies: 1Last Post: 01192011, 09:23 PM
Bookmarks