Results 1 to 12 of 12
Thread: Issues with Division
- 01-31-2011, 01:31 AM #1
Member
- Join Date
- Jan 2011
- Posts
- 7
- Rep Power
- 0
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
- 01-31-2011, 01:57 AM #2
Member
- Join Date
- Jan 2011
- Location
- Gainesville, FL
- Posts
- 45
- Rep Power
- 0
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.
- 01-31-2011, 01:59 AM #3
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,606
- Rep Power
- 5
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
- 01-31-2011, 02:03 AM #4
The return value of the format method is a String, not a double.Java Code:double result = twoDecimals.format((double)num1 / num2);
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.
- 01-31-2011, 02:22 AM #5
Member
- Join Date
- Jan 2011
- Location
- Gainesville, FL
- Posts
- 45
- Rep Power
- 0
- 01-31-2011, 02:46 AM #6
Member
- Join Date
- Jan 2011
- Posts
- 7
- Rep Power
- 0
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?
I believe there's something wrong with the .format syntax because I receive the following error with that.Java Code:import java.text.DecimalFormat; DecimalFormat twoDecimals = new DecimalFormat("#.##"); double correctAnswer = twoDecimals.format(double)(num1 / num2);
'.class' expected
double correctAnswer = twoDecimals.format (double)(num1 / num2);
Thanks for all your help,
KaW
- 01-31-2011, 02:52 AM #7
Did you read my reply? Obviously not otherwise you would have seen that I explained what the problem is.
- 01-31-2011, 03:05 AM #8
Member
- Join Date
- Jan 2011
- Posts
- 7
- Rep Power
- 0
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
- 01-31-2011, 03:07 AM #9
Member
- Join Date
- Jan 2011
- Location
- Gainesville, FL
- Posts
- 45
- Rep Power
- 0
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.Java Code:import java.text.DecimalFormat; DecimalFormat twoDecimals = new DecimalFormat("#.##"); String correctAnswerStr = twoDecimals.format((double)num1 / num2); double correctAnswer = Double.parseDouble(correctAnswerStr);
- 01-31-2011, 03:12 AM #10
Member
- Join Date
- Jan 2011
- Posts
- 7
- Rep Power
- 0
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.
- 01-31-2011, 03:13 AM #11
Why bother?
Just leave it as a String and use the equals method.
- 01-31-2011, 03:32 AM #12
Member
- Join Date
- Jan 2011
- Posts
- 7
- Rep Power
- 0
Similar Threads
-
Help with event to change output value in a division
By benjibabs in forum New To JavaReplies: 4Last Post: 05-07-2010, 11:29 AM -
big double division
By grilo in forum Advanced JavaReplies: 4Last Post: 10-11-2009, 10:07 PM -
Problem with division using doubles
By chrismanahan in forum New To JavaReplies: 3Last Post: 10-10-2009, 09:26 PM -
java division and decimal error
By heartysnowy in forum New To JavaReplies: 5Last Post: 10-07-2009, 04:57 PM -
how to discard remainder on division?
By RobertF in forum New To JavaReplies: 9Last Post: 03-13-2009, 12:20 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks