Calculation still returning Nan

1. Senior Member
Join Date
Dec 1969
Posts
170

Calculation still returning Nan

Hi Guys,<BR><BR>Thanks for the help Bruno.. the calculation is still returning NaN.<BR><BR>function calculate()<BR>{<BR>document.eaf.txtSalTotal.value = (parseFloat(document.eaf.txtCash.value) + parseFloat(document.eaf.txtVehicle.value) + parseFloat(document.eaf.txtAllowances.value) + parseFloat(document.eaf.txtBenefits.value))<BR>}<B R><BR>Can anyone suggest why I would be getting NaN?? How would I utilise IsNaN??<BR><BR>I am going around and around in circles! :)<BR><BR>

2. Senior Member
Join Date
Dec 1969
Posts
555

RE: Calculation still returning Nan

isNaN checks to see if something is NOT a number. So, if you do this:<BR>isNaN("xyz")<BR>it will return true since "xyz" is NOT a number.<BR><BR>If you do this:<BR>isNaN("7") it will return false since "7" CAN be a valid number.<BR><BR>Hope this helps.

3. Senior Member
Join Date
Dec 1969
Posts
1,671

RE: Calculation still returning Nan

You&#039re getting NaN because one of the parseFloats is trying to operate on a non-numeric value; perhaps a space snuck in one of the fields?<BR><BR>You need something like this:<BR><BR>function calculate(){<BR><BR> var cash = document.eaf.txtCash;<BR> var vehicle = document.eaf.txtVehicle;<BR> var allowances = document.eaf.txtAllowances;<BR> var benefits = document.eaf.txtBenefits;<BR> var nTotal = 0;<BR><BR> if (!isNaN(cash.value)){<BR> nTotal += parseFloat(cash.value);<BR> }<BR> if (!isNaN(vehicle.value)){<BR> nTotal += parseFloat(vehicle.value);<BR> }<BR> if (!isNaN(allowances.value)){<BR> nTotal += parseFloat(allowances.value);<BR> }<BR> if (!isNaN(benefits.value)){<BR> nTotal += parseFloat(benefits.value);<BR> }<BR><BR> document.eaf.tstSalTotal.value = nTotal;<BR>}<BR><BR>Good luck!<BR><BR>Mike<BR><BR>BTW, who else hates this non-indenting messageboard for code fragments?

Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•