# Issues with Division

• 01-31-2011, 02:31 AM
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, 02:57 AM
Vase
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.

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, 02:59 AM
doWhile
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, 03:03 AM
Junky
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.
• 01-31-2011, 03:22 AM
Vase
Quote:

Originally Posted by Junky
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.
• 01-31-2011, 03:46 AM
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?
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
• 01-31-2011, 03:52 AM
Junky
Did you read my reply? Obviously not otherwise you would have seen that I explained what the problem is.
• 01-31-2011, 04:05 AM
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, 04:07 AM
Vase
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.
• 01-31-2011, 04:12 AM
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, 04:13 AM
Junky
Why bother?

Just leave it as a String and use the equals method.
• 01-31-2011, 04:32 AM