Java gives the wrong answer in computing

I want to do some calculations in Java,

I know it's not so bad to do something in more steps, but why use additional text if it can be shortened?

If my math is wrong, will someone give me a pointer?

This is a line of code

percent1 = (((totaloutput1Int - Total1Int) / totaloutput1Int) * 100);

also = (((2232 - 1590) / 2232) * 100)

This is a feasible multi - step code

percent1step1 = (totaloutput1Int - Total1Int);

percent1step2 = ((percent1step1 / totaloutput1Int)* 100);

percent1Tv.setText(String.valueOf(percent1step2));

Solution

Change totaloutput1int and total1int from int to double, and everything will work normally

In the first method, int / int results in the rounding of the value This can lead to different results

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>