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."

Code Java:

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.

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

Re: school assignment; decimal rounding

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

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

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.

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?

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.

Re: school assignment; decimal rounding

Quote:

Originally Posted by

**Junky**
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?

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.

Re: school assignment; decimal rounding

Here's my code:

Code Java::

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.

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.