1

For some reason my math just returns 0. The value are set, I have checked.

int currentSize = 4079;
int totalSize = 500802;

int percentage = ((currentSize/totalSize) * 100);
progdialog.setProgress(percentage);

Percentage always equals percentage. Why?

Bryan
  • 2,191
  • 20
  • 28
Evan Darwin
  • 850
  • 1
  • 8
  • 29

8 Answers8

10

The problem, as other have pointed out, is integer division will turn anything less than 1 to zero. This happens before multiplying by 100. You can change the order of operations to get something better:

int percentage = currentSize * 100 / totalSize;

If you are concerned about rounding, you can use

int percentage = (currentSize * 100 + (totalSize >> 1)) / totalSize;

These avoid the expense of working with double or float values.

Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
  • 1
    @agksmehx - In the first example, the multiplication happens first, so division by `totalSize` gives you an int percentage value, which is what OP wants. (Multiplying before dividing is a way to avoid integer division giving a premature zero value.) The second does the same thing but adds half of `totalSize` to the numerator so that after the division, it's like having added 0.5 before taking the integer part of the result. – Ted Hopp Nov 13 '11 at 06:49
  • 1
    +1 this answer if you've ever seen a program grind to a halt because of a tight loop of floating point division for a progress bar. – Keith Layne Nov 13 '11 at 07:47
  • Nice trick, but a more general answer would have been nice too - eg, how to force a floating point division given integer operands. (arrived here via google search, looking for the general answer) – Phil Dec 18 '17 at 23:48
  • 1
    @Phil - The point here was to _avoid_ floating point operations, not to force them! :) But to address your objective: to force an operation to be carried out in floating point, just force at least one of the operands to be a `float` (by casting, say). Note that it does no good to cast the result—it's the operator types that are important. For a multi-operator expression, an analysis of operator precedence will let you determine which operand(s) need to be coerced to `float` so that all the operations that need to be done in floating point are actually carried out in floating point. – Ted Hopp Dec 19 '17 at 00:10
4

you are using 'int's for currentSize and totalSize which results in integer division which removes the fractional part, yielding 0. hence the percentage is always 0.

change it to float percentage = (((float)currentSize/totalSize) * 100); and things will be fine

necromancer
  • 23,916
  • 22
  • 68
  • 115
1

I assume currentSize and totalSize are int.

currentSize = 4079;
totalSize = 500802;

If they are, then currentSize/totalSize is an integer division. The result will have no fractional part (the fractional part is removed, no round up). Therefore the result is 0.

If one of the operand is double, the result of division will have fraction. Therefore, I cast one integer operand to double.

(double) currentSize

After the calculation, if you want the result to store in int, you have to cast (convert double to int; remove fractional part).

int percentage = (int) ((double) currentSize ...

The whole code is:

int currentSize = 3;
int totalSize = 100;

int percentage = (int) ((double) currentSize / totalSize * 100);
System.out.println(percentage);
wannik
  • 12,212
  • 11
  • 46
  • 58
0
  double occupancyRate = 0.0;
  int occupiedRoomsTotal = 12;
  int totalRooms = 20;

  occupancyRate = (((double) occupiedRoomsTotal / totalRooms)) * 100;
  DecimalFormat df2 = new DecimalFormat("#.##");
  System.out.println("Occupancy Rate = " + df2.format(occupancyRate) + "%");
0

Because the result of your calculation is a double with value less than 1. You put it in an integer so it truncates everything behind the decimal separator, resulting in zero. Try storing the value in a double instead.

Phil
  • 2,238
  • 2
  • 23
  • 34
ophychius
  • 2,623
  • 2
  • 27
  • 49
0

If currentSize and totalSize are integers, this calculation will do integer division, which will truncate your fraction down to 0. Use doubles.

Community
  • 1
  • 1
0

Change your code to this:

double percentage = ((double)(currentSize/totalSize) * 100);
progdialog.setProgress(percentage);

Hope it will help yout. :)

Android Killer
  • 18,174
  • 13
  • 67
  • 90
  • That won't help if `currentSize` and `totalSize` are int values. The conversion to `double` will only happen at the end of the (integer) operations on the right. – Ted Hopp Nov 13 '11 at 06:30
-1

Java Division of integers yields zero if both numerator and denominator are both integers and the result is less than 1. Fix:

Make either of the operands to be floating number or double e.g. int x = 1; double y = 3.0;

x/y gives 0.333333

where as 1/3 results in 0.

Yergalem
  • 1,603
  • 18
  • 14
  • don't you mean `float x = 1.0f; float y = 3.0f;` ? – nlloyd Jul 02 '16 at 05:44
  • `int y=3.0` will turn it into `3`, and `x/y` will still be integer division. You would want to say `double y=3.0`. But this has already been answered several times, so I don't know that your answer adds anything here. – Teepeemm Jul 02 '16 at 11:45
  • @nollyd int x = 1 and double y = 3.0 – Yergalem Jul 04 '16 at 05:41