Link to home
Start Free TrialLog in
Avatar of OGSan
OGSanFlag for United States of America

asked on

Javascript sum currency amounts

I have the following routine that generates a "running total" of checkbox menu items (employee concessions), but it is running into two problems: (1) The first item checked does not appear unless another mouse-click is made on the screen, and (2) even though all amounts are currency, the total will occasionally display a bunch of decimal positions (e.g.,  10.59 + 2.25 displays as 12.8300000009).  
Here is the little function:
function checkTotal() {
	document.frmPost.runningTotal.value = '';
	var sum = 0;
	for (i=0;i<document.frmPost.Concession.length;i++) {
	  if (document.frmPost.Concession[i].checked) {
	  	sum = Number(Number(sum) + Number(document.frmPost.Concession[i].value));
	  }
	}
	document.frmPost.runningTotal.value = '$' + sum;
	}

Open in new window

And the field that it is being displayed inside of is defined as:
<input name="runningTotal" type="text">

Open in new window

Any insights would be very much appreciated!
SOLUTION
Avatar of Kevin Cross
Kevin Cross
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of OGSan

ASKER

Super responses!  Thanks SO much for the clear and concise assist.  I changed the ONCHANGE to ONCLICK to fix the first problem, and modified the final line of code in my routine to "sum.toFixed(2)" and all is GOLDEN!  Thank you both for the help!
You're welcome, glad to help.