Chris
Chris

Reputation: 3

Formulating Percentage

I'm in the middle of creating a small lua program. In the program I want to calculate the percentage of hits (numPercent) from the number of hits (numHits) and the number of misses (numMiss).

For instance, if I were to hit the target 5 times and missed 0, it would show the percentage as 100% Hits

How would I formulate this problem?

This is what I got so far, which as you can see is completely incorrect.

 if ( numHit > numMiss) then --calculates percentage
 numPercent = numHit / numMiss * 100/2
 else
 numPercent = numMiss / numHit * 100/2
 end

Could I get some guidance in formulating it correctly?

Upvotes: 0

Views: 7061

Answers (1)

catwell
catwell

Reputation: 7020

numPercent = 100 * numHit / (numHit + numMiss)

You may need to test if "numHit + numMiss" is zero before that, and return whatever you want in that case.

Upvotes: 2

Related Questions