# 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;
}
``````
And the field that it is being displayed inside of is defined as:
``````<input name="runningTotal" type="text">
``````
Any insights would be very much appreciated!
LVL 1
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Chief Technology OfficerCommented:
For the second issue, you can try sum.toFixed(2).  For the first issue (if I understand correctly), you need to trap on the mouse events if you want to see changes before the user changes focus.
0
Fixer of ProblemsCommented:
JavaScript has only integer and floating point arithmetic.  There are many decimal values that can not be exactly represented by floating point arithmetic which is why you get the long decimal numbers at times.  To get two decimal places in your display, use the toFixed() Method shown on this page: http://www.w3schools.com/js/js_number_methods.asp  The 'Tryit' demo makes it clearer.
0

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Author Commented:
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!
0
Fixer of ProblemsCommented: