Results 1 to 8 of 8

Thread: number problem

  1. #1
    click66 is offline Member
    Join Date
    Oct 2010
    Posts
    4
    Rep Power
    0

    Question 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.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  3. #3
    click66 is offline Member
    Join Date
    Oct 2010
    Posts
    4
    Rep Power
    0

    Default

    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!

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  5. #5
    click66 is offline Member
    Join Date
    Oct 2010
    Posts
    4
    Rep Power
    0

    Default

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

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,193
    Rep Power
    19

    Default

    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

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,436
    Blog Entries
    7
    Rep Power
    20

    Default

    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

  8. #8
    click66 is offline Member
    Join Date
    Oct 2010
    Posts
    4
    Rep Power
    0

    Default

    Quote Originally Posted by Darryl.Burke View Post
    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. )

Similar Threads

  1. Replies: 0
    Last Post: 06-28-2010, 12:30 PM
  2. Finding a number in array close to another number
    By SteroidalPsycho in forum New To Java
    Replies: 2
    Last Post: 02-15-2010, 12:37 AM
  3. Printing the Number of Times a Number in a Range Shows up
    By space4rent00 in forum New To Java
    Replies: 1
    Last Post: 02-05-2010, 10:42 PM
  4. Replies: 2
    Last Post: 10-14-2008, 09:32 PM
  5. Replies: 2
    Last Post: 11-11-2007, 08:07 AM

Posting Permissions

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