How can I calculate percent

Discussion in 'Plugin Development' started by FreeMineLP, Jan 13, 2015.

Thread Status:
Not open for further replies.
  1. Offline

    FreeMineLP

    Hi,
    I want to calculate a percent number using 2 varibales.
    I want to calculate kill/death count. But I don't know how I do it.

    Sorry for my "good" english :D (German people)
     
  2. Offline

    16davidjm6

    @FreeMineLP

    double percent = ((double)kill/(double)death_count) * 100

    Looks like your math skills might not be so great either. May I suggest learning basic math skills before trying to take on java?
     
  3. Offline

    NathanWolf

    ... and make sure you check for death_count == 0, in which case you just want to use "100".
     
  4. Offline

    FreeMineLP

    Thanks :)

    My math skills are big enough i am on a german highschool but i dont know much enough how i use my math skills in java :D
     
  5. Offline

    Konato_K

    Skionz, timtower and teej107 like this.
  6. Offline

    ColonelHedgehog

    The formula for percentage is number/goal * 100. 25/100 * 100 = 25%, 5/234 = 0.02136752137%, etc.
     
  7. Offline

    FreeMineLP

    I know -.-
     
  8. Offline

    xMakerx

    LOL, thanks for the laugh.
     
    Skionz likes this.
  9. Offline

    dsouzamatt

    Correct me if I'm wrong, but wouldn't you want to use 1 instead?
     
  10. Offline

    NathanWolf

    Not if you're multiplying by 100 for display purposes (unless you still multiply by 100).

    ... how is this thread still alive? XD
     
  11. Offline

    dsouzamatt

    @NathanWolf I meant in the denominator of the fraction; you still multiply by 100 to calculate a percentage.
     
  12. Offline

    NathanWolf

    I mean it depends on how you do it. The first suggestion was this:

    double percent = ((double)kill/(double)death_count) * 100

    Which I would change to

    double percent = death_count <= 0 ? 100 : ((double)kill/(double)death_count) * 100;

    You could just as easily change it to

    double percent = (death_count <= 0 ? 1 : ((double)kill/(double)death_count)) * 100;

    But it doesn't really matter, mainly just make sure to not divide by zero.
     
  13. Offline

    Konato_K

    @NathanWolf I might have gone crazy, but doesn't divide by zero returns a NaN with doubles/floats or something? I recall having a NaN value once by doing this, I also recall a RuntimeException with ints.
     
  14. Offline

    leon3001

    Konato_K likes this.
Thread Status:
Not open for further replies.

Share This Page