gpt4 book ai didi

Java:整数除以更大的整数会导致0

转载 作者:行者123 更新时间:2023-12-02 03:38:54 24 4
gpt4 key购买 nike

所以我写了这个小函数,它应该将图像缩放到宽度 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.. 向下舍入为零)。

为了避免整数除法,请将其中任何一个强制转换/转换为 double 。您将看到结果。

关于Java:整数除以更大的整数会导致0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37081950/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com