division double android

。_饼干妹妹 提交于 2019-12-04 04:31:11

问题


I have a problem with my division on android:

    double test= 100/ 1280;
    double test2 = 0.2354;
    System.out.println(test);
    System.out.println(test2);

I have

0.0
0.2354

I'd like to have

0.078125
0.2345

Thanks


回答1:


Try this

 double test= (double) 100/ 1280;



回答2:


if you specify one of the numbers in your division with a decimal, i.e.

double test = 100.0/1280;

It will give you the desired output.

The reason you are not getting the correct result, is that when dividing two ints, the resulting type will also be an int. In order to avoid this, you have to typecast one of the operands in the division to a double, or - as a shorter solution when you are explicitly setting the number in the operation and not using variables - you can add the ".0" to one of the numbers.



来源:https://stackoverflow.com/questions/15494184/division-double-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!