-2

I am trying to return a double so that it rounds up to the tenths. I have:

 roundedTemp = (double)Math.round(tempNum *10)/10;
 return roundedTemp;

What am I doing wrong? I get it back to 2 decimal places...

fifamaniac04
  • 2,343
  • 12
  • 49
  • 72
  • 5
    A `double` is a _base 2_ IEEE 754 floating point number; meaning it can have, and often has, trouble being rounded to an exact decimal ("deci" as in "ten") number. – fge Apr 08 '14 at 17:21
  • What do you mean by "I get it back to 2 decimal places"... what are you doing that makes it appear that you're getting 2 decimal places? Also, what is `tempNum`? (That is, what is its value? It would help to know if it's a very large or very small number.) – ajb Apr 08 '14 at 17:24
  • I want a value such that it returns #.# – fifamaniac04 Apr 08 '14 at 17:25
  • 1
    Please be clearer. You get `#.##` from _what_? `System.out.println`? `String.format`? We are not mind readers. – ajb Apr 08 '14 at 17:26
  • Do you mean that `System.out.println(roundedTemp)` prints two digits after the decimal point? Could you give at least one or two examples of values of `tempNum` for which that happens? – David K Apr 08 '14 at 17:34

1 Answers1

0

Use DecimalFormat.

DecimalFormat df = new DecimalFormat("#.#");
javaGeek
  • 304
  • 1
  • 4
  • 11
  • That may be the correct answer if he just wants to *output* one decimal place. But it looks like he wants (or might want) an actual rounded value, which is a different question. – ajb Apr 08 '14 at 17:35