By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,016 Members | 2,297 Online
Bytes IT Community
+ Ask a Question
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
  1. var a = this.getField("LO");
  2. var b = this.getField("PP");
  3. var c = this.getField("AEC");
  4. var d = this.getField("UM");
  5. var e = this.getField("TOT");
  6. event.value = Math.round(a.value+b.value+c.value+d.value*e.value);
  7.  
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
Share this Question
Share on Google+
3 Replies


iam_clint
Expert 100+
P: 1,208
I would do something like this.
Expand|Select|Wrap|Line Numbers
  1. var a = this.getField("LO");
  2. var b = this.getField("PP");
  3. var c = this.getField("AEC");
  4. var d = this.getField("UM");
  5. var e = this.getField("TOT");
  6. if (a=="") { a = 0; }
  7. if (b=="") { b = 0; }
  8. if (c=="") { c = 0; }
  9. if (d=="") { d = 0; }
  10. if (e=="") { e = 0; }
  11. var answer = Math.floor(a.value+b.value+c.value+d.value*e.value);
  12. //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
  13. event.value = Math.round(answer);
  14.  
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
  1.  
  2. var a = this.getField("LO");
  3. if (a=="") { a = 0.00; }
  4.  
  5.  
Do I understand you correctly?
Nov 1 '07 #3

iam_clint
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

Post your reply

Sign in to post your reply or Sign up for a free account.