Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 11 of 11

Thread: school assignment; decimal rounding

  1. #1
    Member
    Join Date
    Aug 2013
    Posts
    101
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Cool school assignment; decimal rounding

    Hi, I've been trying to get this problem to work for two days for my school. I'm just starting computer science class in Java. I really really really need help on my code. I've sent the teacher an email but I'm scared I won't get a response in time. I think I'm good at this class but I've only been doing it for a week or so. I have very little time to finish. Please help.

    By the way, I know how to round to one digit. Just not a variable number of digits.

    Here is the problem:

    "In the following segment of code, a and b are non-negative doubles and d is an int between 0 and 5 (inclusive). Write code that assigns to b the result of rounding a to d decimal places."

     
    Here is my code so far:
     
    b = a * Math.pow(10, d);
    b += 5; 
    b = (int)b;
    b *= 10;
    b = (int)b;
    b /= Math.pow(10, d);
    b = (int)(b * Math.pow(10, d + 1));
    b /= Math.pow(10, d + 2);

    Help appreciated. Thanks.


  2. #2
    Member
    Join Date
    Jul 2013
    Posts
    47
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: school assignment; decimal rounding

    Just looking at the API, I'm not even sure how that Math.pow() is working at all. There are apparently three classes in the Math package. Both BigInteger and BigDecimal have pow() methods, so how is it even differentiating which one you're calling?

    Either way, it looks like you're using a two-argument version in any case. It's Math.BigDecimal.pow(int n, MathContext mc) that takes two args, and as you can see, the second one is a MathContext object.

    So if I'm reading that right, you would need to feed a MathContext object to it.

    Looks like you need a MathContext with a "precision," whatever that is (I'm learning this too lol), appropriate for rounding to the nearest tenth, then feed that to your code.

    You have to write a method that creates a MathContext object USING the value of d as its precision, and set b equal to Math.round(a, mc).

    At least, so it seems.

    -summit45

  3. #3
    Member
    Join Date
    Aug 2013
    Posts
    101
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: school assignment; decimal rounding

    @summit45 Thanks for the help. I would really appreciate more experienced advice though. But you can still help.

  4. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: school assignment; decimal rounding

    @summit: There is a Math.pow method which takes two doubles, and the result is a to the power of b.

    @ghostheadx: Try first writing out on paper what steps you would need to do to round a number to a given decimal place. Also, there is a Math.round() method you might want to look at instead of casting to int (casting to int truncates the decimals, not rounds it).

  5. #5
    Member
    Join Date
    Aug 2013
    Posts
    101
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: school assignment; decimal rounding

    I don't think he wants me to use Math.round() because he's never taught me that before. Are you sure there's no way to do it with casting? I know it works for one decimal place.

  6. #6
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: school assignment; decimal rounding

    What kind of "rounding" do you need?

    Is 4.637 rounded to 2 decimal places 4.63 or 4.64 rounded up?
    Improving the world one idiot at a time!

  7. #7
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: school assignment; decimal rounding

    What your doing now will probably work 9 times out of 10, but it's highly suspect. It's also bad practice. I don't think the teacher will give you any problems over using Math.round(), but you'll have to ask them directly.

  8. #8
    Member
    Join Date
    Aug 2013
    Posts
    101
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: school assignment; decimal rounding

    Quote Originally Posted by Junky View Post
    What kind of "rounding" do you need?

    Is 4.637 rounded to 2 decimal places 4.63 or 4.64 rounded up?
    4.637 rounded would be 4.64.

    --- Update ---

    I'll see what he says. I sent him an email. If he says "no" which I believe he won't, what would you say to do?

  9. #9
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: school assignment; decimal rounding

    I would add 0.5 then cast to int to simulate rounding. This covers most cases, but I don't think this is completely fool proof.

  10. #10
    Member
    Join Date
    Aug 2013
    Posts
    101
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Cool Re: school assignment; decimal rounding

    Here's my code:

     
    b = a + (5 * Math.pow(10, -d));
    b *= Math.pow(10, d);
    b = Math.round(b * d);
    b /= Math.pow(10, d);

    I think I'm doing SOMETHING wrong still. What you guys have said has helped a lot.

  11. #11
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: school assignment; decimal rounding

    Carefully evaluate what your code is doing: use print statements or use a debugger and step through your code. What outputs are you getting? What is expected?

    Try doing what I suggested above: write out the steps on paper (no actual Java code) you need to do to get a number rounded to d decimal places.

Similar Threads

  1. Java assignment school
    By Ur Nerd in forum Paid Java Projects
    Replies: 3
    Last Post: September 7th, 2013, 03:22 AM
  2. [SOLVED] Rounding Decimal Numbers Question
    By Nuggets in forum Java Theory & Questions
    Replies: 5
    Last Post: March 19th, 2012, 04:12 PM
  3. Problem with School Assignment.
    By SaltSlasher in forum What's Wrong With My Code?
    Replies: 5
    Last Post: February 13th, 2012, 08:22 AM
  4. School Assignment AHH!
    By Europa in forum Loops & Control Statements
    Replies: 8
    Last Post: January 20th, 2012, 09:19 AM
  5. Java program for to implement supermarket menu
    By 5723 in forum AWT / Java Swing
    Replies: 1
    Last Post: April 14th, 2009, 03:14 AM

Tags for this Thread