Rouding

• 09-14-2013, 06:37 AM
nepperso
Rouding
I'm currently taking a class in learning java.

Anyways, I am doing a code that was provided and has an error.
We are suppose to review and figure out why it's giving the error and how to fix it.

You coded the following in class Test.java:
Code:

```int a = 32; int b = 10; double c = a / b; System.out.println( “The value of c is “ + c );```
The code compiles properly and runs, but the result is not what you
expected. The output is
The value of c is 3.0
You expected the value of c to be 3.2. Explain what the problem is and
how to fix it.

So the problem is the left over value. In the chapter I read it spoke about using % to bring out the remainder. But all the options I could think of wouldn't work.
This is what I wrote with some modification on the original script
Code:

```        int         a = 32, b = 10;         double c = (double)a/b;         c%=c;                      //tried to float c but that gives the same answer.  The issue I believe is a rounding                  // I know % would be right trick to pull the extra digits.          System.out.println("The value of c is " + c);                 System.exit(0);```
I would appreciate any assistance on this

I also tried playing around with
Code:

```        int a, b;         double c;                 b = 10;         a = 32;         c = a%b;```
• 09-14-2013, 03:09 PM
jim829
Re: Rouding
Try your next to last example again but remove the c%=c statement.

Regards,
Jim