1

I just wanted to ask a quick question regarding the Math.round method. I'm trying to compute the division of two ints into a double. The equation looks like this: 199/39. When I do this it returns 5.0 as the answer. I know the answer should be 5.1 with some more decimals. I have been told to use the Math.round method to round it to the nearest tenth, but I have no idea how to accomplish this. Should I change that double variable to a int and make it int/int=int? I'm not sure how Math.round even works to get 5.1 as I've read it only rounds to the nearest integer not decimal point. Any help would be fantastic.

P.S This is homework, but I ask only because I can't find any information in my notes, slides, or book on how to use Math.round.

Travis M
  • 58
  • 1
  • 1
  • 5

4 Answers4

1

You don't need Math.round() to get a resultant decimal value. If you divide an int by an int, you will get an int. if you want a decimal, then cast double to one of the input values. Then you will get a double as a result.

(double) 199 / 39
199.0 / 39

// both return
5.102564102564102
hexacyanide
  • 88,222
  • 31
  • 159
  • 162
1

I know the answer should be 5.1 with some more decimals.

Not with integer division it shouldn't. 5 is correct.

If you want a floating-point answer, you need to provide at least one floating-point operand, e.g. 199/39.0.

You can then format that for printing with as many or few decimal places you like, with System.printf() or DecimalFormat.

You can't round the floating-point value itself to decimal places, because it doesn't have decimal places, it has binary places.

See this question for a full discussion, especially my answer there.

Community
  • 1
  • 1
user207421
  • 305,947
  • 44
  • 307
  • 483
0

Your specific answer:

roundedNumber = (double)Math.round(unRoundedNumber * 10) / 10;

In general, the equation is:

roundedNumber = 
(double)Math.round(unRoundedNumber * Math.pow(10, digitsToRoundTo)) 
/ Math.pow(10, digitsToRoundTo);
Michael Yaworski
  • 13,410
  • 19
  • 69
  • 97
  • Nope. Refutation [here](http://stackoverflow.com/a/12684082/207421). – user207421 Oct 19 '13 at 01:35
  • 1
    @EJP So give me a ratio that it will not work for. This method, to 8 decimal places of 199.0/39, works like it should. If it's wrong more than 90% of the time, then provide at least one example of when it is wrong. – Michael Yaworski Oct 19 '13 at 02:05
0

Thanks for all the help. After reading your posts and looking at it some more I ended up doing ratio=((double)199/39) and then going ratio=(double)Math.round(ratio*10)/10. Doing that got me the 5.1 i was looking for.

Travis M
  • 58
  • 1
  • 1
  • 5
  • 1
    It didn't really, it just printed that way. Try `System.out.printf("%.4f", (double)199/39);` for proof. – user207421 Oct 19 '13 at 01:24
  • All in all though when I ran it through my loop it reported the results I was looking for. I was shoehorned into using math.round for my project. Truth be told I wouldn't want to use math.round, but I had no choice. – Travis M Oct 19 '13 at 01:33