0

I have a double array that has the population of some cities in the first array, and the populations of the country those cities are in in the 2nd array.

double[][] population = new double[][]{ 
        {24153000,18590000,18000000,14657000,14543000,13617000,13197596,12877000,12784000,12400000,12038000,
        11908000,11548000,11035000,10608000,10355000,10290000,10152000,10125000,9752000},

        {1384688986,1384688986,207862518,81257239,162951560,126168156,143964513,105920222,
        1384688986,1296834042,207652865,1384688986,1384688986,1296834042,1384688986,207862518,50791919,1384688986,86300000,31773839
        }

I am trying to find the percent of a Countries population that lives in the city. So I have a for loop that divides array 1 at some index by the equivalent index at array 2. The body of the loop says

double percent= (population[0][i]*100f)/population[1][i];

Yet I keep getting integer division. For example, for dividing the first elements of both arrays, I get 2 instead of 1.744.

Can anyone tell me why this is happening? I am getting whole numbers only even though I am dividing doubles and not ints.

Edit: Here is the rest of the loop. There is some extra stuff, such as i'm only supposed to print out the cities that have over 10m people. Also some formatting.

for (int i = 0; i < population[1].length; i++) {
            if (population[0][i] > 10000000) {
          double percent = (population[0][i]*100f)/population[1][i];
                System.out.printf("%10.0f", percent);
                System.out.println();
            }
  • Possible duplicate of https://stackoverflow.com/questions/13668007/odd-behaviors-when-dividing-doubles-in-java – dmitryro Sep 09 '18 at 00:55
  • This looks fine, although there's no need to use the float literal, `100f`. Just 100 or 100.0 would be fine. What leads you to think the value is 2? How are you printing it? – RaffleBuffle Sep 09 '18 at 01:21
  • Works fine using System.out.println – dustytrash Sep 09 '18 at 01:27
  • Could you post your for loop? Something else is going on. If it was integer division, 1.744 would be 1, not 2. Your 'percent' var should be correct for the code you are showing. – Zachary Sep 09 '18 at 01:40
  • @Zachary I have posted the rest of my loop. – Josh Jeabos Sep 09 '18 at 01:49
  • 1
    That dup link is not correct. (It never was that problem!) The real problem here is the format. "%10.0f" means decimal floating point with 10 characters width and zero digits after the decimal point. That results in rounding to the nearest whole number. – Stephen C Sep 09 '18 at 02:22
  • @Stephen 100 would be fine as it's an operand in an expression containing a `double`, and so would undergo a widening primitive conversion to double – RaffleBuffle Sep 09 '18 at 02:50
  • Ooops ... I missed that population was declared as `double[][]`. So technically that is OK. But 100.0 is stylistically preferable ... for obvious reasons. – Stephen C Sep 09 '18 at 03:21

2 Answers2

1

It's the way you are printing it out. Try

System.out.println(percent); 

or check out the documentation for the Formatter

https://docs.oracle.com/javase/8/docs/api/java/util/Formatter.html

Zachary
  • 324
  • 1
  • 13
1

The problem is the way that you are printing the number:

   System.out.printf("%10.0f", percent);

The format specifier "%10.0f" says:

  • decimal floating point
  • 10 characters width
  • zero digits after the decimal point.

That results in rounding to the nearest whole number.

Alternatives:

  • Increase the zero in the format
  • Remove it, and use the default (up to 6 digits after the decimal point)
  • Just use println(percent) ... which will display the number to the full significant precision ... and no more.

Note: it is advisable use 100.0 (type double) rather than 100f (type float) when forcing an expression to use floating point calculation. It is better to do the calculation with double precision, especially if you are going to assign the result to a double.

In this case, you are not forcing to floating point, since population is already floating point. However:

  • It is stylistically preferable to use 100.0 to flag to the reader that the calculation is being done in double precision.

  • It is possible that a float literal is less precise than the corresponding double literal ... though not in this case.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216