Dividing two integers in Java gives me 0 or 100?

后端 未结 5 953
悲&欢浪女
悲&欢浪女 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:42

    Try this:

    int x = a * 100 / b;
    

    The idea is, you are first doing a / b, and because it's an integer operation, it'll round the result to 0. Doing a * 100 first should fix it.

提交回复
热议问题