Numbers are int
by default in Java. So when you do 5/9
, since they're both int
s, you'll get 0.something, and since this is an int
, only 0 will be stored. Solution:
double s = 5.0/9; //or 5d/9
Note that you can only explicitly cast one side, the other will be implicitly cast.
Now the calculation will be in a double
manner and you'll get the desired result.