Dividing two integers in Java gives me 0 or 100?

后端 未结 5 966
悲&欢浪女
悲&欢浪女 2020-12-01 20:52

I\'m trying to divide two integers and multiply by 100 but it keeps giving only 0 or 100. Can someone help me?

    int x= (a/b)*100;

if a w

5条回答
  •  再見小時候
    2020-12-01 21:25

    Integer division has no fractions, so 500 / 1000 = 0.5 (that is no integer!) which gets truncated to integer 0. You probably want

    int x = a * 100 / b;
    

提交回复
热议问题