I'm trying to divide an integer by an integer in Java and return a long result. I tried the following, but I keep getting "0".
System.out.println(Long.valueOf(5) / Long.valueOf(18));
System.out.println((long)5 / (long)18);
I'm trying to divide an integer by an integer in Java and return a long result. I tried the following, but I keep getting "0".
System.out.println(Long.valueOf(5) / Long.valueOf(18));
System.out.println((long)5 / (long)18);
Longs are still integral, you need a type that can handle decimals, use either
float
ordouble
.You don't need a long, you need a double.
or
Of course this will work too:
Long
is for "long" integer numbers. You should usefloat
ordouble
instead.It seems everyone has given the right answer, but you should note you can also do
Where f makes it a float
where d makes it a double