Flash Website Builder- Trendy Site Builder is a Flash Site Building tool that helps users build stunning websites. Check Out Custom Custom Logo Design by LogoBee. Website Design and Free Logo Templates available.
 CodingForums.com How do you calculate percentages in Javascript?

Before you post, read our: Rules & Posting Guidelines

Enjoy an ad free experience by logging in. Not a member yet? Register.
 04-12-2011, 11:31 PM PM User | #1 NKeuxmuis New Coder   Join Date: Feb 2011 Posts: 23 Thanks: 4 Thanked 0 Times in 0 Posts 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
 04-13-2011, 12:56 AM PM User | #2 bullant Banned   Join Date: Feb 2011 Posts: 2,699 Thanks: 13 Thanked 395 Times in 395 Posts For debugging add the alert in red and your error should become clear. Code: ```alert(f+' '+x.length); percent=f/x.length * 100```
 04-13-2011, 01:04 AM PM User | #3 NKeuxmuis New Coder   Join Date: Feb 2011 Posts: 23 Thanks: 4 Thanked 0 Times in 0 Posts Hi, thanks for the reply I tried what you said but I am still none the wiser on why I am getting this error. That code you told me just said the values were 3 and 8 which I already knew they would be.
 04-13-2011, 01:16 AM PM User | #4 bullant Banned   Join Date: Feb 2011 Posts: 2,699 Thanks: 13 Thanked 395 Times in 395 Posts There must be something else going on in your code that you haven't posted. In this demo, the output in the alert() is 37.5 Code: ``` ```
 04-13-2011, 03:01 AM PM User | #5 NKeuxmuis New Coder   Join Date: Feb 2011 Posts: 23 Thanks: 4 Thanked 0 Times in 0 Posts Thanks for your reply, I did cut out some code because I thought it was irrelevant and would be confusing for those who didn't write it. Here is the full code which basically uses Javascript to open an XML file. The displaymember function is then supposed to count how many males/females are in the xml file and display the number of females as a percentage. The code seems to work perfectly when I checked it until I get to the "percent=f/x.length * 100" part. That is where everything goes wrong. Code: ```
```
 04-13-2011, 05:24 AM PM User | #6 bullant Banned   Join Date: Feb 2011 Posts: 2,699 Thanks: 13 Thanked 395 Times in 395 Posts Looking at your code, it appears your xml file structure is something like this Code: ``` Male Female Male ``` I named the above test file testXML.xml in the demo below. The demo below, based on your posted code but with a couple of "tweaks", works fine with the above xml data and outputs 33.33 as the percentage female. Code: ```
```
 Users who have thanked bullant for this post: NKeuxmuis (04-13-2011)
 04-13-2011, 06:01 PM PM User | #7 NKeuxmuis New Coder   Join Date: Feb 2011 Posts: 23 Thanks: 4 Thanked 0 Times in 0 Posts Thanks Bullant that worked perfectly, you are a lifesaver. I'm completely new to Javascript and this helped me accomplish what I was trying to achieve
 04-14-2011, 12:35 AM PM User | #8 bullant Banned   Join Date: Feb 2011 Posts: 2,699 Thanks: 13 Thanked 395 Times in 395 Posts you're welcome

 Bookmarks

 Tags mathematics, maths, numbers, percentage, percentages

 Thread Tools Rate This Thread Rate This Thread: 5 : Excellent 4 : Good 3 : Average 2 : Bad 1 : Terrible

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is Off Forum Rules
 Forum Jump User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home :: Client side development     JavaScript programming         DOM and JSON scripting         Ajax and Design         JavaScript frameworks         Post a JavaScript     HTML & CSS     XML     Flash & ActionScript         Adobe Flex     Graphics and Multimedia discussions     General web building         Site reviews         Building for mobile devices :: Server side development     Apache configuration     Perl/ CGI     PHP         Post a PHP snippet     MySQL         Other Databases     Ruby & Ruby On Rails     ASP     ASP.NET     Java and JSP     Other server side languages/ issues         ColdFusion         Python :: Computing & Sciences     Computer Programming     Computer/PC discussions     Geek News and Humour Web Projects and Services Marketplace     Web Projects         Small projects (quick fixes and changes)         Medium projects (new script, new features, etc)         Large Projects (new web application, complex features etc)         Unknown sized projects (request quote)         Vacant job positions         Looking for work/ for hire         Project collaboration/ partnership         Paid work offers and requests (Now CLOSED)     Career, job, and business ideas or advice     Domains, Sites, and Designs for sale         Domains for sale         Websites for sale         Design templates and graphics for sale :: Other forums     Member Offers     Forum feedback and announcements

All times are GMT +1. The time now is 05:50 PM.