Java:将Integer除以更大的Integer会导致0

  • 本文关键字:Integer Java java android
  • 更新时间 :
  • 英文 :


所以我写了一小段函数,它应该将图像缩放到800的宽度(高度可以是任何东西)。

然而,当我用整数(800)除以更大的整数(例如1080)时,结果是0。

是什么原因造成的?

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

我的意思是,双被叫比是0。

800/1080是整数除法,因为两个操作数都是int,因此它解析为零(约0.740.四舍五入为零)。

若要避免整数除法,请将其中任意一个强制转换为双精度。你会看到结果的。

最新更新