0

Possible Duplicate:
Why does (360 / 24) / 60 = 0 … in Java

This line of code:

System.out.println ("array[j], "+array[j]+", divided by sum, "+sum+", equals: array[j]/sum: "+ array[j]/sum) ;

is yeilding this line of text:

array[j], 21, divided by sum, 100, equals: array[j]/sum: 0

why is it doing this? (everything is right eccept that the answer should be .21)

Mrigank Pawagi
  • 892
  • 1
  • 8
  • 26
David
  • 14,569
  • 34
  • 78
  • 107

4 Answers4

3

Are you sure that your array is not integer ?

if it's, try using double.

Ahmet Kakıcı
  • 6,294
  • 4
  • 37
  • 49
2

I'm assuming that aray is an int[] and sum is an int. In this case, Java will perform integer division, which results in 0 in this case.

VeeArr
  • 6,039
  • 3
  • 24
  • 45
1

Others noted the cause. To fix, (double) aray[j]/sum.

Nikita Rybak
  • 67,365
  • 22
  • 157
  • 181
0

Dividing integers will get you an integer answer rounded down to the first whole number. If you want a decimal result, you have to make it 21.0/100.0.

edl
  • 3,194
  • 22
  • 23