user860511
user860511

Reputation:

Java Division error

I have the following variables:

int first = 0;
int end = 0;

Declare in the public class.

Within a method:

double diff = end / first;
double finaldiff = 1 - diff;

The end variable on System.out.println is 527, the first is 480.

Why is the answer for diff coming out as 1? It should be 1.097916667, I thought using a double would enable me to calculate into decimals?

Upvotes: 4

Views: 3099

Answers (1)

Ry-
Ry-

Reputation: 225164

Dividing two ints will get you an int, which is then implicitly converted to double. Cast one to a double before the divison:

double diff = (double)end / first;

Upvotes: 5

Related Questions