1. Member 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?  Reply With Quote

2. Can you use BigDecimal? If so, that's what I'd use here.  Reply With Quote

3. Member 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!  Reply With Quote

4. 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.  Reply With Quote

5. Member 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...?  Reply With Quote

6. Forget about BigDecimal for the moment (sorry Fubarable).

it must round to 40 numbers after the comma and show
Given an arbitrary fraction, how would you do this with pen and paper, one digit at a time? How would you stop the process if there aren't any more digits? How would you count 40 digits after the decimal point and put a "..." if the next digit isn't zero?

Do that, and then translate your steps to Java code. (hint: multiply, divide and modulo -- * / % -- and of course if(...) or maybe while(...))

db  Reply With Quote

7. 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  Reply With Quote

8. Member Join Date
Oct 2010
Posts
4
Rep Power
0  Originally Posted by Darryl.Burke Forget about BigDecimal for the moment (sorry Fubarable).

Given an arbitrary fraction, how would you do this with pen and paper, one digit at a time? How would you stop the process if there aren't any more digits? How would you count 40 digits after the decimal point and put a "..." if the next digit isn't zero?

Do that, and then translate your steps to Java code. (hint: multiply, divide and modulo -- * / % -- and of course if(...) or maybe while(...))

db
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. )  Reply With Quote Posting Permissions

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