Results 1 to 8 of 8
Thread: number problem
 10102010, 10:18 PM #1Member
 Join Date
 Oct 2010
 Posts
 4
 Rep Power
 0
number problem
I've a problem ( and i'm a 2 weeks working with java en still learning), the user must give a number p en q, then it must divide p / q ,
if it is a whole number then it may not show the .
for example 10 / 2 this would be 5
but if it is 33/12 then it would me 2.75
No problem with that but extra is that if it is a number like
1/3 = 0,333333333 .. it keeps going, then it must round to 40 numbers after the comma and show … ( three dots, so that they now it is endless. )
I don't know in Java how to make the 1/3 part… I sit stuck at this.
Someone know how to solve this?
Thanks in advantage.

Can you use BigDecimal? If so, that's what I'd use here.
 10102010, 10:24 PM #3Member
 Join Date
 Oct 2010
 Posts
 4
 Rep Power
 0
I'm new, but my university sais that everything can be used... so yes but can you explain how it works then?
Thnx for the help btw!

BigDecimal will allow you to do basic math operations with an almost arbitrary degree of accuracy. It's helpful for instance if you need to do critical financial calculations, the only downside being that there's a memory and performance cost, but this shouldn't matter to you.
 10102010, 10:33 PM #5Member
 Join Date
 Oct 2010
 Posts
 4
 Rep Power
 0
uhu, i've read a article about it on java website, but still don't see how it is usefull in this case? Because still i can't check if it isn't broken after a couple of numbers...?
 10112010, 07:30 AM #6
Forget about BigDecimal for the moment (sorry Fubarable).
it must round to 40 numbers after the comma and show …
Do that, and then translate your steps to Java code. (hint: multiply, divide and modulo  * / %  and of course if(...) or maybe while(...))
db
 10112010, 09:43 AM #7
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,309
 Blog Entries
 7
 Rep Power
 20
Let p and q be two positive integers; the fraction p/q either has repeating fractions or not. If not the denominator q has the form 2^n*5^m, otherwise there are repeating fractions. The length of those fractions can easily be found by repeatedly dividing p by q and keep the remaninder. If the remainder has occurred before count the number of modulo operations, that's the length of the repeating fractions.
e.g. p= 5, q= 39; 50%39 == 11, 110%39 == 32, 320%39 == 8, 80%39 == 2, 200%39 == 5; so the length of the repeating fractions equals 5.
kind regards,
Jos
 10112010, 03:27 PM #8Member
 Join Date
 Oct 2010
 Posts
 4
 Rep Power
 0
Well if the number is 0,333333335 then i would multiply with 10 then i have 3 , then i would multiply the number with 100 but the i have 33 . And this i would repeat max 40 times, but if the last number is a 0 then I would stop the while lus.. not? But my problem is you get first time 3, second time 33, how can i check that it is zero? ( maybe stupid question but... don't see it yet. )
Similar Threads

large number of DB writes: java heap space problem in using transactioncommit
By narimani in forum JDBCReplies: 0Last Post: 06282010, 12:30 PM 
Finding a number in array close to another number
By SteroidalPsycho in forum New To JavaReplies: 2Last Post: 02152010, 12:37 AM 
Printing the Number of Times a Number in a Range Shows up
By space4rent00 in forum New To JavaReplies: 1Last Post: 02052010, 10:42 PM 
[SOLVED] Problem hadling number(money) > 214,74,83,647.99
By playwin2 in forum New To JavaReplies: 2Last Post: 10142008, 09:32 PM 
Problem calling classes to flip coin x number of times and record heads or tails
By adlb1300 in forum New To JavaReplies: 2Last Post: 11112007, 08:07 AM
Bookmarks