View Single Post
Old 04-12-2011, 10:31 PM   PM User | #1
NKeuxmuis
New Coder

 
Join Date: Feb 2011
Posts: 23
Thanks: 4
Thanked 0 Times in 0 Posts
NKeuxmuis is an unknown quantity at this point
How do you calculate percentages in Javascript?

Can anyone help me with the following Javascript. I am going crazy trying to figure this out because it should be easy. Basically I want to calculate the percentage of a number, but I'm getting a really weird result. In the following code f is equal to 3 and x.length is equal to 8. The part that isn't working is emphasised in bold. Basically 3/8 * 100 should result in 37.5 but the result I am getting with the following code is 7934570.3125. How do you calculate this percentage in Javascript?

Code:
function displaymember()
{
var m = 1;
var f = 1;
for(i=0;i<x.length;i++)
 {
 
	sex=(x[i].getElementsByTagName("sex")[0].childNodes[0].nodeValue);

if (sex=="Male")
  {
  m++;
  }
else
  {
  f++;
  }

percent=f/x.length * 100

document.getElementById("fixed").innerHTML=percent;
   }
}
NKeuxmuis is offline   Reply With Quote