5

Possible Duplicate:
Java Integer Division, How do you produce a double?

double wang = 3 / 2;
Log.v("TEST", "Wang: " + Double.toString(wang));

Logcat output...

07-04 09:01:03.908: VERBOSE/TEST(28432): Wang: 1.0

I'm sure there's an obvious answer to this and probably I'm just tired from coding all night but this has me stumped.

Community
  • 1
  • 1
Paul Blaine
  • 435
  • 2
  • 6
  • 9
  • 1
    http://stackoverflow.com/questions/3144610/java-integer-division-how-do-you-produce-a-double http://stackoverflow.com/questions/2187482/is-long-x-1-2-equal-to-1-or-0-and-why – Jacob Jul 04 '11 at 08:08
  • 1
    try `double wang = 3.0 / 2;` instead. You are using integer division so you're getting expected result. – Rekin Jul 04 '11 at 08:09
  • Expected, i.e. 3 / 2 = round(1.5) = 1 – Rekin Jul 04 '11 at 08:09
  • Thanks for all the super quick answers everybody. I guess it makes sense if they are ints. – Paul Blaine Jul 04 '11 at 08:13

5 Answers5

17

In many languages, Java being one of them, the way you write a number in an expression decides what type it gets. In Java, a few of the common number types behave like this1:

// In these cases the specs are obviously redundant, since all values will be
// cast correctly anyway, but it was the easiest way to show how to get to the
// different data types :P
int i = 1;
long l = 1L;
float f = 1.0f;   // I believe the f and d for float and double are optional, but
double d = 1.0d;  // I wouldn't bet on what the default is if they're omitted...

Thus, when you declare 3 / 2, you're really saying (the integer 3) / (the integer 2). Java performs the division, and finds the result to be 1 (i.e. the integer 1...) since that's the result of dividing 3 and 2 as integers. Finally, the integer 1 is cast to the double 1.0d which is stored in your variable.

To work around this, you should (as many others have suggested) instead calculate the quotient of

(the double 3) / (the double 2)

or, in Java syntax,

double wang = 3.0 / 2.0;

1 Source: The Java Tutorial from Oracle

Tomas Aschan
  • 58,548
  • 56
  • 243
  • 402
  • Thanks again for all the answers everyone gave. You explained it very well. – Paul Blaine Jul 04 '11 at 08:33
  • 2
    The default when `f` and `d` are omitted is `double`. [*"A floating-point literal is of type `float` if it is suffixed with an ASCII letter `F` or `f`; **otherwise its type is `double` and it can optionally be suffixed with an ASCII letter `D` or `d`**."*](https://docs.oracle.com/javase/specs/jls/se8/html/jls-3.html#jls-3.10.2) – Radiodef Jun 15 '17 at 13:15
  • I know this is an old question, forgive me for asking this but I came across it and was wondering something. Why does: `(double)(3/2)` = `1.0`? Is that because it is converting to double AFTER the "integer" division is done? Following that logic, would `(double)(3.0/2.0)` be the only time you'll get `1.5`? – GµårÐïåñ Mar 16 '19 at 19:29
7

Integer division of 3 by 2 is equal to 1 with residue of 1. Casting to double gives 1.0

Boris Pavlović
  • 63,078
  • 28
  • 122
  • 148
6

3 and 2 are integer constants and therefore 3 / 2 is an integer division which results in 1 which is then cast into a double. You want 3.0 / 2.0

ChrisWue
  • 18,612
  • 4
  • 58
  • 83
5

Try: double wang = 3.0 / 2.0;

Eng.Fouad
  • 115,165
  • 71
  • 313
  • 417
2

That's the expected behaviour. "3" and "2" are both int values, and when you perform 3 / 2 the result will also be an int value which gets rounded down to 1. if you cast both to double before you perform the division then you'll get the result that you expect:

double wang = (double)3 / (double)2;
Mark Allison
  • 21,839
  • 8
  • 47
  • 46