Josh Jeabos
Josh Jeabos

Reputation: 61

Dividing 2 double elements in an array keep resulting in unintentional rounding

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();
            }

Upvotes: 0

Views: 193

Answers (2)

Stephen C
Stephen C

Reputation: 718688

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.

Upvotes: 1

Zachary
Zachary

Reputation: 323

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

Upvotes: 1

Related Questions