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)

KaW

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.

3. Moderator
Join Date
Jul 2010
Location
California
Posts
1,641
Rep Power
9
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. 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. Member
Join Date
Jan 2011
Location
Gainesville, FL
Posts
45
Rep Power
0
Originally Posted by Junky
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.

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

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:

Then I'm storing the user input as:

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);

KaW

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

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

9. Member
Join Date
Jan 2011
Location
Gainesville, FL
Posts
45
Rep Power
0
Java Code:
```import java.text.DecimalFormat;

DecimalFormat twoDecimals = new DecimalFormat("#.##");
String correctAnswerStr = twoDecimals.format((double)num1 / num2);
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. 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.

11. Why bother?

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

12. Member
Join Date
Jan 2011
Posts
7
Rep Power
0
Vase,

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

Thanks again.

Posting Permissions

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