Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to divide two longs and get the value?

Tags:

java

I need to compute probability of an integer and a long. but I always get 0.

int a = 234;
long b = 123453344L

float c = a /b;

How to get it right in Java?

like image 657
user697911 Avatar asked Oct 14 '25 07:10

user697911


1 Answers

You need to either cast one of them as a float, or declare one of the variables to be a float from the start. Otherwise, Java's integer division takes over, and just as an int divided by an int must be an int, that would apply to longs as well.

float c = (float) a / b;
like image 182
rgettman Avatar answered Oct 16 '25 21:10

rgettman