0

so I wrote this small piece of function which is supposed to scale image to width of 800 (height can be anything).

However - when I divide integer (800) by bigger Integer (1080 for example), the result comes out as 0.

What causes this?

            double ratio = 800 / bit.getWidth();
            double newHeight = ratio * bit.getHeight();
            double newWidth = ratio * bit.getWidth();
            Log.d("New Values:", String.valueOf(newHeight) + "/ " + String.valueOf(newWidth));
            scaleBitmap(bit, (int)newHeight, (int)newWidth);

I mean the double called ratio comes out as 0.

arleitiss
  • 107
  • 2
  • 13

1 Answers1

2

800/1080 is integer division cause both operands are int's, hence it resolves to zero (approx 0.740.. rounded down to zero).

To avoid the integer division, cast/convert any one of them to double. You'll see the result.

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307