Results 1 to 12 of 12
  1. #1
    Join Date
    Jan 2011
    Posts
    7
    Rep Power
    0

    Default Issues with Division

    Hello,

    I'm new to Java and I'm having issues with limiting the number of decimal points that a variable stores.

    Here's what I've been asked to do.

    Generate two random numbers (integers) and then have the first divided by the second. I'm not having an issue with this so far.

    My problem comes when I'm asked to have the user type in the answer. Obviously 24/3 = 8, the user types in 8. But what happens when it's 8 / 6? The system is storing 1.33333333333333 but the user types in 1.33 and he/she's told it's the wrong answer.

    So, how can I limit the system to only store the answer with 2 digits (1.33)?

    Do I have to convert the answer to a String? (saw this out there somewhere)

    Thanks for your help,

    KaW

  2. #2
    Vase is offline Member
    Join Date
    Jan 2011
    Location
    Gainesville, FL
    Posts
    45
    Rep Power
    0

    Default

    If you are randomly generating two integers, the result of the division will be an integer. In this case, 8 / 6 is 1.

    If you are casting the result to a double, you could use DecimalFormat (java.text.DecimalFormat) to have it round to two decimal places.

    Java Code:
    int num1 = 8, num2 = 6;
    
    DecimalFormat twoDecimals = new DecimalFormat("#.##");
    double result = twoDecimals.format((double)num1 / num2);

    The result will be formatted to 1.33.

  3. #3
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default

    You can use a few tricks, one of which involves casting to an integer to get the rounded floating point value:

    double d = 8/3d;
    d = ((int)( 100 * d ))/100d;

    Another of which is to compare the values using a String, using a DecimalFormat object to format the double to a given precision

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    Java Code:
    double result = twoDecimals.format((double)num1 / num2);
    The return value of the format method is a String, not a double.

    Beware a gotcha here. Remember to do the cast and/or formatting at the correct time other wise you will end up with 1.0 instead of 1.33.

  5. #5
    Vase is offline Member
    Join Date
    Jan 2011
    Location
    Gainesville, FL
    Posts
    45
    Rep Power
    0

    Default

    Quote Originally Posted by Junky View Post
    Java Code:
    double result = twoDecimals.format((double)num1 / num2);
    The return value of the format method is a String, not a double.

    Beware a gotcha here. Remember to do the cast and/or formatting at the correct time other wise you will end up with 1.0 instead of 1.33.
    You're right about the return value; my bad.


    Also, just to clarify:
    (double)num1 / num2 will result in 1.33.
    (double)(num1 / num2) will result in 1.0.

  6. #6
    Join Date
    Jan 2011
    Posts
    7
    Rep Power
    0

    Question

    Thanks for the suggestions,

    however there was a lot going on there.

    Let me see if I can integrate what you posted to what I already have.

    I'm storing what the system thinks is the correct answer as you guessed it:

    double correctAnswer = 0;

    Then I'm storing the user input as:

    double userAnswer = 0;

    So what you're proposing is to make these changes?
    Java Code:
    import java.text.DecimalFormat;
    
    DecimalFormat twoDecimals = new DecimalFormat("#.##");
    double correctAnswer = twoDecimals.format(double)(num1 / num2);
    I believe there's something wrong with the .format syntax because I receive the following error with that.
    '.class' expected
    double correctAnswer = twoDecimals.format (double)(num1 / num2);

    Thanks for all your help,

    KaW

  7. #7
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    Did you read my reply? Obviously not otherwise you would have seen that I explained what the problem is.

  8. #8
    Join Date
    Jan 2011
    Posts
    7
    Rep Power
    0

    Default

    Junky,

    I did, and I tried both syntaxes to no avail.

    Actually I've tried:

    double correctAnswer = twoDecimals.format(double)(num1 / num2);
    double correctAnswer = twoDecimals.format(double) num1 / num2;
    double correctAnswer = twoDecimals.format((double)(num1 / num2));
    double correctAnswer = twoDecimals.format double (num1 / num2);

    So, being new to this, I thought that I'd try to repost with more information to make sure that I understood what was going on.

    Just trying to learn something new here.

    Thanks for the help,

    KaW

  9. #9
    Vase is offline Member
    Join Date
    Jan 2011
    Location
    Gainesville, FL
    Posts
    45
    Rep Power
    0

    Default

    Java Code:
    import java.text.DecimalFormat;
    
    DecimalFormat twoDecimals = new DecimalFormat("#.##");
    String correctAnswerStr = twoDecimals.format((double)num1 / num2);
    double correctAnswer = Double.parseDouble(correctAnswerStr);
    Your parentheses are out of order for what to format, which is why you're getting that error. Also, as corrected by Junky, DecimalFormat returns a String. You can compare it to an integer/double input by parsing it.

  10. #10
    Join Date
    Jan 2011
    Posts
    7
    Rep Power
    0

    Default

    Vase,

    OK, so I will be converting it to a String, and then parsing it back to a double.

    Cool, let me try it out.

    Thanks.

  11. #11
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    Why bother?

    Just leave it as a String and use the equals method.

  12. #12
    Join Date
    Jan 2011
    Posts
    7
    Rep Power
    0

    Default

    Vase,

    That was it. Once I converted my input to a double it started working just like I wanted it to.

    Thanks again.

Similar Threads

  1. Help with event to change output value in a division
    By benjibabs in forum New To Java
    Replies: 4
    Last Post: 05-07-2010, 12:29 PM
  2. big double division
    By grilo in forum Advanced Java
    Replies: 4
    Last Post: 10-11-2009, 11:07 PM
  3. Problem with division using doubles
    By chrismanahan in forum New To Java
    Replies: 3
    Last Post: 10-10-2009, 10:26 PM
  4. java division and decimal error
    By heartysnowy in forum New To Java
    Replies: 5
    Last Post: 10-07-2009, 05:57 PM
  5. how to discard remainder on division?
    By RobertF in forum New To Java
    Replies: 9
    Last Post: 03-13-2009, 01:20 PM

Posting Permissions

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