12
double[] arrayName = new double[10]; 
arrayName[0] = (14/49)*100;

Trying to get percentage of two numbers, 14 / 49, and add results to an array. All it returns is 0.0. Same thing if I use float instead of double. New to Java.

AMIC MING
  • 6,306
  • 6
  • 46
  • 62
batoutofhell
  • 1,279
  • 3
  • 12
  • 17

5 Answers5

18

Either cast the ints to double, or just use doubles.

For example:

double[] arrayName = new double[10]; 
resultValue[0] = (14.0/49.0)*100;

or:

double[] arrayName = new double[10]; 
resultValue[0] = ((double)14/49)*100;
Nora Powers
  • 1,597
  • 13
  • 31
  • Not working for some reason. Here's what I have: double[] arrayName = new double[10]; arrayName[0] = (double)(14/49)*100; System.out.println("Result: "+arrayName[0]);' Returns 0.0. Sorry about formatting in this comment – batoutofhell Feb 07 '13 at 00:10
  • Working, thanks @mmnumbp! But I had to do it as '((double)14/(double)49)*100;' The first method didn't work. – batoutofhell Feb 07 '13 at 00:16
  • Ah sorry, I placed (double) in the wrong spot. Here, I'll edit the answer. – Nora Powers Feb 07 '13 at 00:19
  • 1
    @batoutofhell: you can use backticks to format code inline in comments. – halfer Feb 07 '13 at 09:57
10

How you see it:

(double/double)*double

How the JVM sees it

(int/int)*int

From the the JLS. int to double is a widening conversion. From §5.1.2:

Widening primitive conversions do not lose information about the overall magnitude of a numeric value.

[...]

Conversion of an int or a long value to float, or of a long value to double, may result in loss of precision-that is, the result may lose some of the least significant bits of the value. In this case, the resulting floating-point value will be a correctly rounded version of the integer value, using IEEE 754 round-to-nearest mode (§4.2.4).

If you replace one of those with a double literal (like adding .0 or cast one of them to double and you should get the result you expect.

Jason Sperske
  • 29,816
  • 8
  • 73
  • 124
3

Like this:

arrayName[0] = (14/49)*100.0; // put ".0" after any of the numbers

or this:

arrayName[0] = (double)(14/49)*100;
xagyg
  • 9,562
  • 2
  • 32
  • 29
3

At least one of your values should be a double:

double[] arrayName = new double[10]; 
arrayName[0] = (14d/49d)*100;
// or...
// arrayName[0] = (14d/49)*100;
// or...
// arrayName[0] = (14/49d)*100;
// or...
// arrayName[0] = (14.0/49)*100;
// or...
// arrayName[0] = (14/49.0)*100;
// or...
// arrayName[0] = (14/(double)49)*100;
// or...
// arrayName[0] = ((double)14/49)*100;
// or...
// arrayName[0] = ((double)14/(double)49)*100;
Richard JP Le Guen
  • 28,364
  • 7
  • 89
  • 119
2

As many others have pointed, you just have to set the constants as doubles:

arrayName[0] = (14*100)/49.0;

This way, you convert only the last computation to double instead of the beginning , and reduce the number of floating point calculations.

Btw, if you wish, you can also have the result as an integer by using the same method of swapping the order and use multiplication before division, for example: 14*100/49 instead of (14/49)*100.

halfer
  • 19,824
  • 17
  • 99
  • 186
android developer
  • 114,585
  • 152
  • 739
  • 1,270