1

So this is a basic question, I have 2 int variables or field as it is called in Java. One is total score the other is game rounds. I am using double which takes decimal numbers as the data type for average variable. But I'm not getting the correct result.

int totalScore = 75;
int gameRounds = 2;

public void calculateAverage() {
    double average = totalScore / gameRounds;
    System.out.println(average);

}

The result is: 37.0, why isn't it the result 37.5, even though I'm using the correct datatype?

CookieMonster
  • 241
  • 1
  • 6
  • 19
  • 2
    Possible duplicate of [Division of integers in Java](http://stackoverflow.com/questions/7220681/division-of-integers-in-java) – Yoland Gao Mar 06 '16 at 01:18
  • I have searched before adding this question and didn't find anything similar before you sent it. Even though both questions involve using division on integers, I would argue my answer and the people who have responded has given a more detailed description on how casting works and the right way to use it. Which will help others searching for a similar question. – CookieMonster Mar 06 '16 at 01:45
  • Possible duplicate of [Why is the result of 1/3 == 0?](http://stackoverflow.com/questions/4685450/why-is-the-result-of-1-3-0) – fabian Mar 06 '16 at 20:32

1 Answers1

2

So I figured it out! I tried casting the value to double, it works perfectly now. The result is 37.5. But I would like to get other answers if they have better solution to this.

  double average = (double)totalScore / gameRounds;

NOTE: When I used brackets around the int values it didn't work as shown below. So make sure to do as I showed above.

double average = (double)(totalScore / gameRounds);
CookieMonster
  • 241
  • 1
  • 6
  • 19
  • 1
    Just so you know the difference, in your first example, `totalScore` is cast, then the division happens. In your second example, the division happens, then the result of the division is cast. – Carcigenicate Mar 06 '16 at 01:19
  • The key is to understand the rules for the type in which arithmetic is done. It depends on the types of the inputs, not on what is being done with the result. Dividing a double by an int, as in your first example, is done as double. Dividing int by int, as in the second example, is done as an int division. – Patricia Shanahan Mar 06 '16 at 01:21
  • Ok, so the correct way would be to cast both int variables? – CookieMonster Mar 06 '16 at 01:21
  • @CookieMonster It is sufficient to cast one of them. Mixed double/int division is done by converting the int to double and dividing in double. – Patricia Shanahan Mar 06 '16 at 01:22
  • @CookieMonster You only need to cast 1. If either of the operands are non-integral, it will use floating-point arithmetic, which gives a non-integral answer. – Carcigenicate Mar 06 '16 at 01:23
  • Your second example gives a "wrong" answer because the result is rounded *then* cast. – Carcigenicate Mar 06 '16 at 01:24
  • @Patricia Shanahan and Carcigenicate I'll just stick with casting one of them to double since its sufficient. Thanks your explanation helped me understand it – CookieMonster Mar 06 '16 at 01:28