Results 1 to 2 of 2

Thread: Rouding

  1. #1
    nepperso is offline Member
    Join Date
    Sep 2013
    Posts
    1
    Rep Power
    0

    Default 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:
    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
    Java 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
    Java Code:
            int a, b;
            double c;
            
            b = 10;
            a = 32;
            c = a%b;
    Last edited by nepperso; 09-14-2013 at 07:01 AM.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,510
    Rep Power
    5

    Default Re: Rouding

    Try your next to last example again but remove the c%=c statement.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Tags for this Thread

Posting Permissions

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