percentage of two int?

151,391

Solution 1

If you don't add .0f it will be treated like it is an integer, and an integer division is a lot different from a floating point division indeed :)

float percent = (n * 100.0f) / v;

If you need an integer out of this you can of course cast the float or the double again in integer.

int percent = (int)((n * 100.0f) / v);

If you know your n value is less than 21474836 (that is (2 ^ 31 / 100)), you can do all using integer operations.

int percent = (n * 100) / v;

If you get NaN is because wathever you do you cannot divide for zero of course... it doesn't make sense.

Solution 2

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;

Solution 3

One of them has to be a float going in. One possible way of ensuring that is:

float percent = (float) n/v * 100;

Otherwise, you're doing integer division, which truncates the numbers. Also, you should be using double unless there's a good reason for the float.

The next issue you'll run into is that some of your percentages might look like 24.9999999999999% instead of 25%. This is due to precision loss in floating point representation. You'll have to decide how to deal with that, too. Options include a DecimalFormat to "fix" the formatting or BigDecimal to represent exact values.

Share:
151,391
Baruch
Author by

Baruch

check out my website! http://isolatedpixel.com/ Check out my IndieDB project!

Updated on July 09, 2022

Comments

  • Baruch
    Baruch almost 2 years

    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'm probably way off...) for example:

    int correct = 25;
    int questionNum = 100;
    float percent = correct/questionNum *100;
    

    This is how I thought I could do it, but it didn't work... I want to make the decimal (if there is one) into a percent out of 100 for example in this case it is %25. any ideas anyone?

    Here is the correct code (thanks to Salvatore Previti!):

    float correct = 25;
    float questionNum = 100;
    float percent = (correct * 100.0f) / questionNum;
    

    (btw, I am making a project using this for a quiz checking program that is why I need the percentage or decimal)

  • Ryan Stewart
    Ryan Stewart over 12 years
    That's one of the many ways to do it. The important thing is that one of the numbers used in the division is a floating point number.
  • Salvatore Previti
    Salvatore Previti over 12 years
    Yes ... you can do (n / (float)v) * 100;
  • JB Nizet
    JB Nizet over 12 years
    100F is sufficient. No need for the .0.
  • Baruch
    Baruch over 12 years
    @SalvatorePreviti it gives me NAN as the answer when im using the float. with the int, it doesn't work at all..
  • Salvatore Previti
    Salvatore Previti over 12 years
    If v is zero, this calculation doesn't make sense, and is the only way you can get NaN (a number divided zero is not a number).
  • Baruch
    Baruch over 12 years
    I get NAN as an answer.. any ideas?
  • David R Tribble
    David R Tribble over 12 years
    No, the operation does not require any float values. See @Eric's answer.
  • Baruch
    Baruch over 12 years
    v is equal to 4, let me check it again.
  • Baruch
    Baruch over 12 years
    no, still not working and i checked it and v is equal to 4 and n is equal to 1
  • Baruch
    Baruch over 12 years
    i changed n to a float and now i got the answer as infinity -_-
  • Baruch
    Baruch over 12 years
    I got it!!!!!! i changed every variable into a float and now it is working!!! thanks!!
  • Ryan Stewart
    Ryan Stewart over 12 years
    @Loadmaster: That only works if v is evenly divisible by n. Otherwise you'll get rounding errors. The OP explicitly asked for a "decimal or percentage", and since this is for a quiz application, a non-fp answer won't work very well unless you want to set strict limits on the number of questions that can be on a quiz.
  • Eric
    Eric over 12 years
    @Baruch: Both n and v must be 0, if you're getting NaN
  • Andrii Abramov
    Andrii Abramov over 7 years
    Please, be careful with integer overflow. In case of overflow, you'll probably get negative numbers.