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

1. 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. 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. Re: school assignment; decimal rounding

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

4. 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. 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. 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?

7. 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. Re: school assignment; decimal rounding

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?

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