David Garcia
David Garcia

Reputation: 2696

Percentage difference between two values

I am using the following simple JavaScript code to calculate the percentage difference between two values:

subOBS001 = (vars.obs001acount) - (vars.obs001asent); 
divOBS001 = (subOBS001) / (vars.obs001asent); 
modOBS001 = (divOBS001) * (100);

if (modOBS > 30) {%> <%= modOBS %><span id="greenStatus">&#8226;</span> <%} else {%> <%= modOBS %>

Is there a simpler way to perform my calculation? Have it all in one single line using parenthesis?

Upvotes: 3

Views: 4511

Answers (1)

famousgarkin
famousgarkin

Reputation: 14126

Your calculation done on one line, if that is what you want:

modOBS001 = (vars.obs001acount - vars.obs001asent) / vars.obs001asent * 100;

And if your math is correct and you want the value of vars.obs001asent represent 100%.

Upvotes: 7

Related Questions