These forums are now Read Only. If you have an Acrobat question, ask questions and get help from one of our experts.

JAVASCRIPT CALCULATION

phornung2
Registered: Mar 2 2010
Posts: 23
Answered

I was hoping someone might be kind enough to look at the script below, and tell me if there is a way to actually get it to work. I am calculating the sum of two fields, then dividing by the sum of two other fields.

/*this.rawValue = (Number(Field_1_1.rawValue) + Number(Field1_2.rawValue)) / (Number(Field_1_3.rawValue) + Number(Field_1_4))

*/

Thanks in advance for any help!!

**** Ok - I was able to get it to work by doing this:

this.rawValue = Number(Field_1_1.rawValue) + Number(Field_1_2.rawValue) / Number(Field_1_3.rawValue) + Number(Field_1_4.rawValue);

...Unless the user enters a zero instead of an actual number. If they enter a zero, it does not perform the calculation. Please, any help would greatly be appreciated. Thanks!

My Product Information:
LiveCycle Designer, Windows
phornung2
Registered: Mar 2 2010
Posts: 23
Ok - I was able to get it to work by doing this:

this.rawValue = Number(Field_1_1.rawValue) + Number(Field_1_2.rawValue) / Number(Field_1_3.rawValue) + Number(Field_1_4.rawValue);

...Unless the user enters a zero instead of an actual number. If they enter a zero, it does not perform the calculation. Please, any help would greatly be appreciated. Thanks!
gkaiseril
Online
Expert
Registered: Feb 23 2006
Posts: 4307
What happens when you divide by zero in Excel?

What happens in the real world when you divide by zero or what value do you get when you divide by zero?

Many computer programs treat an empty or null value as zero.

George Kaiser

phornung2
Registered: Mar 2 2010
Posts: 23
Hi gkaiseril,

Thanks so much for replying. Yes, I understand this. However, only one of the two fields might be zero. For example, in the script below, one might enter a zero in Field_1_3, but will enter a number for Field_1_4 (i.e., 0 + 4 = 4). There should always be a total of some number. Any further suggestions?


this.rawValue = Number(Field_1_1.rawValue) + Number(Field_1_2.rawValue) / Number(Field_1_3.rawValue) + Number(Field_1_4.rawValue);
gkaiseril
Online
Expert
Registered: Feb 23 2006
Posts: 4307
And when the form is cleared there is a real chance for division by zero until a number is entered into Field_1_3 or Field_1_4.

Also your computation might be ambiguous. Possible interpretations depending upon how you assume the operators are ordered:

Field_1_1 + (Field_1_2 / Field_1_3) + Field_1_4 // JavaScript assumed ordering

(Field_1_1 + Field_1_2 )/ Field_1_3 + Field_1_4

Field_1_1 + (Field_1_2 / (Field_1_3 + Field_1_4)

((Field_1_1 + Field_1_2) / Field_1_3) + Field_1_4

(Field_1_1 + Field_1_2 / (Field_1_3)+ Field_1_4)

Field_1_1 + (Field_1_2 / (Field_1_3 + Field_1_4))

Do you get any error popping up when you enter preview in LiveCycle Designer?
Do you get any error in the JavaScript console?

With your script as written I only get an answer when a value is entered into Field_1_3.

George Kaiser

phornung2
Registered: Mar 2 2010
Posts: 23
Many thanks for your second reply. No, I don't get an error when I preview the PDF form. What I'm trying to do is first SUM the fields Number(Field_1_1.rawValue) + Number(Field_1_2.rawValue) , then Divide that total with the SUM total of Number(Field_1_3.rawValue) + Number(Field_1_4.rawValue). Sort of like this:

(Field_1_1 + Field_1_2) / (Field_1_3 + Field_1_4) =

In some instances, the user may not enter anything or a zero in Field 1_3 or Field 1_4. However, they WILL enter a number in one of those fields (Field 1_3 , Field 1_4)

I apologize, as you can tell, I'm just learning...
Thanks again
gkaiseril
Online
Expert
Registered: Feb 23 2006
Posts: 4307
Then you need to add the appropriate parenthesis to order the calculations or you could create variables to preform the calculations in the necessary order.

But as long as Field_1_3 and Field_1_4 total zero or are both empty, treated like zero, you will have a zero value for a divisor in your calculation.

// using FormCalcvar sum1 = Field_1_1 + Field_1_2var sum2 = Field_1_3 + Field_1_4// if sum2 is not zero perform divisionif ( sum2 ne 0) thensum1 / sum2elsenullendif

George Kaiser

phornung2
Registered: Mar 2 2010
Posts: 23
Hi gkaiseril,

Again, thank you so much. I think this is really close. Unfortunately, if a user enters a zero in one of the fields for Sum2 I receive an error Arithmetic underflow/overflow error. The total for Sum2 will not be zero, but a user could possibly enter a zero in ONE of the two fields (not both) for Sum2. I hope this makes sense.
gkaiseril
Online
Expert
Registered: Feb 23 2006
Posts: 4307
If you cut and pasted the code as provided and the field names are correct, you should have no problem.

The variable name being tested for zero is 'sum2' not 'Sum2'.

George Kaiser

phornung2
Registered: Mar 2 2010
Posts: 23
Hi gkaiseri,

Yes, it worked! I'm not sure why i received the error message the first time. I reentered the code, made sure I switched back to form calc for the language..and it worked perfectly. Thanks so much!