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

Calculation issues

Alysande
Registered: Oct 2 2008
Posts: 7
Answered

I am having issues with getting a simple addition calculation to work correctly. Ok, this will take a bit to explain the details so bear with me.

I have a text field (ST.0) which is calculating the total of 5 other text fields (SL, SMo.0, SR.0, SMi.0 and SA) as well as adding 5 to the total if a check box (T.0) is checked. Here is the code for ST.0:

var f = this.getField("T.0");
var g = this.getField("SL");
var h = this.getField("SMo.0");
var i = this.getField("SR.0");
var j = this.getField("SMi.0");
 
if (f.isBoxChecked(0))
{
event.value = 5 + g.value + h.value + i.value + j.value + this.getField("SA").value
}
else
{
event.value = g.value + h.value + i.value + j.value + this.getField("SA").value
}

Fields SL, SMo.0 and SA are automatically filled from other fields in the document, while SR.0 and SMi.0 are filled by user input.

Here is the problem:

If SL = x, SMo.0 = y and SA = z (where x,y and z are numbers), and SR.0 and SMi.0 have nothing in them (not even a 0), then the output for ST.0 is (1+x+y)0z, ie if x=1, y=2 and z=3 then ST.0 is 33. However, if SR.0 and SMi.0 have a 0 in them, the calculation is correct. Is there any way to have this calculate without having to enter 0 into SR.0 and SMi.0?

My Product Information:
Acrobat Pro 8.1, Windows
maxwyss
Registered: Jul 25 2006
Posts: 255
The effect you encounter is a consequence of the "loosely typedness" of JavaScript, which means that the type of a variable/value is assigned whenever it is used, and depending on the actual value. When the value can be understood as a number, it may be of type "number", otherwise, it will be of type "string". Now, the empty value can not be understood as a number, but as the empty string. This makes the addition become a concatenation of strings.

So much for a "complicated" explanation.

What it means to general use, is to force the values to be of type "number". And the easiest way to do it is multiplying it with 1... so, instead of

foo.value

you would write

foo.value*1

And that will do it.

Hope this can help.

Max Wyss.
Alysande
Registered: Oct 2 2008
Posts: 7
Thanks very much! That has solved my problem so simply and easily :D