440,016 Members | 2,297 Online Need help? Post your question and get tips & solutions from a community of 440,016 IT Pros & Developers. It's quick & easy.

# Javascript calculation help

 P: 6 Hi guys, I have run into a problem and I can't figure out what's happening or why this script won't work. I am a self-taught scripter and only know what I use really. Any help would be appreciated. OK, here's the problem: I have a total of 5 dollar fields that will be calculated into a 6th dollar field. An example of what I need is like -------- field1 + field2 + field3 + field4 * field5. The answer to all of that gets put into the 6th field, which will also need to be rounded to the nearest whole \$. The only thing I want rounded is the answer, not the individual fields themselves. Also, one thing that I know for sure is wrong (and something I don't know how to do) is the fact that all the fields are optional. Meaning, maybe there will be input in them, and maybe there won't. I just have no idea how to script that. Here's what I've got that isn't working-------------- Expand|Select|Wrap|Line Numbers var a = this.getField("LO"); var b = this.getField("PP"); var c = this.getField("AEC"); var d = this.getField("UM"); var e = this.getField("TOT"); event.value = Math.round(a.value+b.value+c.value+d.value*e.value);   I have tried putting the individual values into ( ) by themselves, which doesn't make a difference. ----------One thing that I noticed that was happening is that when you only enter a value in the first 2 fields, it would get multiplied by 10 for some reason. Then the 3rd field would bring it back to the correct answer, and then the 4th wouldn't have any effect. (Using the value of 1 in all fields.) So 1+1 would show, a total of 20. 1+1+1 would be 3. 1+1+1+1 would be 3. ---------- Any help would be appreciated guys. Thank you!!! Nov 1 '07 #1
3 Replies

 Expert 100+ P: 1,208 I would do something like this. Expand|Select|Wrap|Line Numbers var a = this.getField("LO"); var b = this.getField("PP"); var c = this.getField("AEC"); var d = this.getField("UM"); var e = this.getField("TOT"); if (a=="") { a = 0; } if (b=="") { b = 0; } if (c=="") { c = 0; } if (d=="") { d = 0; } if (e=="") { e = 0; } var answer = Math.floor(a.value+b.value+c.value+d.value*e.value); //var answer = Math.floor(a.value+b.value+c.value+d.value)*e.value; //depends on how you want it calculated a.value+b.value+c.value+d.value = blah * e.value is how this is your other one could be interpreted d.value*e.value+c.value+b.value+a.value event.value = Math.round(answer);   Nov 1 '07 #2

 P: 6 Thanks iam_clint. That pushed me in the right direction. Response very much appreciated!!! Looks like I'll meet my deadline! : ) So to use the if statement to make a field equal 0.00 if there is no input, would I have to name the variable the field I am working with and then use the if statement you gave me? Example: Expand|Select|Wrap|Line Numbers   var a = this.getField("LO"); if (a=="") { a = 0.00; }     Do I understand you correctly? Nov 1 '07 #3

 Expert 100+ P: 1,208 yes, also 0.00 is the same as just 0. And you are putting a space [ /CODE] like this you need to take that space out for posting here. Nov 1 '07 #4 