percentage of two int?

前端 未结 5 689
说谎
说谎 2020-12-12 23:59

I want to get two ints, one divided by the other to get a decimal or percentage. How can I get a percentage or decimal of these two ints? (I\'m not sure if it is right.. I\'

5条回答
  •  悲&欢浪女
    2020-12-13 00:38

    Two options:

    Do the division after the multiplication:

    int n = 25;
    int v = 100;
    int percent = n * 100 / v;
    

    Convert an int to a float before dividing

    int n = 25;
    int v = 100;
    float percent = n * 100f / v;
    //Or:
    //  float percent = (float) n * 100 / v;
    //  float percent = n * 100 / (float) v;
    

提交回复
热议问题