Using Maths in Javascript problems

Results 1 to 2 of 2

Thread: Using Maths in Javascript problems

  1. #1
    Join Date
    Dec 1969

    Default Using Maths in Javascript problems

    Hi Guys,<BR><BR>I need a little bit of help with a function.<BR><BR>I want a "running total" of 4 input fields.<BR><BR>So far I have only been able to get this working using 2 fields as the function seems to ignore the rest.<BR><BR>Below is the code, the references are all correct and I have tried countless syntax changings .. can anyone offer a solution?<BR><BR>Thanks<BR>Renee :))<BR><BR>function calculate()<BR>{<BR>var Cash = document.eaf.txtCash.value;<BR>var Vehicle = document.eaf.txtVehicle.value;<BR>var Allowances = document.eaf.txtAllowances.value;<BR>var Benefits = document.eaf.txtBenefits.value;<BR><BR>document.ea f.txtSalTotal.value = ((Cash) + (Vehicle) + (Allowances) + (Benefits))<BR>}<BR><BR>This one will add the cash and vehicle fields to each other.. eg. 3 + 5 = 35. Not mathematically correct. If I add eval to the start of each name I get a NaN reply in my total field.<BR><BR>I am knowledge limited to onmouseovers.. sorry if I seem vague.. :)<BR><BR>Thanks!!! :)

  2. #2
    Bruno Santos Guest

    Default RE: Using Maths in Javascript problems

    hi Renee!<BR><BR>Use the parseFloat function, which converts string values to floating point numbers:<BR><BR>document.eaf.txtSalTotal.value = (parseFloat(Cash) + parseFloat(Vehicle) + parseFloat(Allowances) + parseFloat(Benefits))<BR><BR>but be careful, because if parseFloat cannot do the conversion, it returns NaN, so it is a good idea to use the isNaN function too.<BR><BR>Bruno (

Posting Permissions

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