Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Math addition

Status
Not open for further replies.

fergy1

IS-IT--Management
Jul 25, 2003
32
0
0
US
I have 6 fields for company ownership. They are decimal fields since an owner can have a fractional ownership of a company. However all 6 fields need to add up to 100 if they dont i need to alert them with my validate script. I have included some alert script i am using already so you can see the style i have. If you can help that would be great!
Code:
if (theForm.Pres.value + theForm.Vice_Pres.value + theForm.Sec.value + theForm.chairman.value + theForm.Other.value != 100)
  {
    alert("Please be sure the ownershp adds to 100 on the \"Sock Owned\" field.");
    return (false);
  }
 
Compare result of operation on floating point numbers to an integer is a risky buisiness. A more reliable way is to make them each a number of fixed decimal points then compare.
[tt]
if (theForm.Pres.value.toFixed(3) + theForm.Vice_Pres.value.toFixed(3) + theForm.Sec.value.toFixed(3) + theForm.chairman.value.toFixed(3) + theForm.Other.value.toFixed(3) != 100.000)
[/tt]
You have to make sure as a matter of course each of those values can convert to number.
 
What happens if ther are null? Will i ignore those? Because not all those fields have to be completed. Just whaatever the do have has to equal 100
 
They won't be null, just empty string which is fine.
 
I see what you meant. You have to convert number string to number and empty to 0 by each applying:
[tt] var x=theForm.Sec.value
x=(x=="")?0:parseFloat(x).toFixed(3)
[/tt]
...etc. for each of them. Sorry for overlooking that part.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top