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

Addition calc inaccurate when one element is zero value

David Dunn
Registered: Oct 28 2010
Posts: 116
Answered

I have several times encountered this, where one of the elements between the first and last in a formula is zero and causes the formula not to add the values following the 0 value, and I would like to know how to deal with this.
 
In the example below, the value of "cpl" is zero and "tax" is $3.00, and the total includes only the values of policy + search. If I swap the positions of "cpl and "tax", then the total includes the value of "tax."
 
var policy = this.getField("num.SS.Expense.Title.Policy").value
var search = this.getField("num.SS.Expense.Title.Search").value
var tax = this.getField("num.SS.Expense.Title.StateFee").value
var cpl = this.getField("num.SS.Expense.Title.CPL").value
 
event.value = policy + search + cpl + tax
 

David D

My Product Information:
Acrobat Pro 9.4, Windows
George_Johnson
Expert
Registered: Jul 6 2008
Posts: 1875
Accepted Answer
Explicitly convert each value to a number using the unary + operator to avoid string concatenation:

// Get field values, as numbers
var policy = +getField("num.SS.Expense.Title.Policy").value
var search = +getField("num.SS.Expense.Title.Search").value
var tax = +getField("num.SS.Expense.Title.StateFee").value
var cpl = +getField("num.SS.Expense.Title.CPL").value



gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4307
I think you will find the value is not zero but a null character string. You are assuming an empty field is a numeric zero, but it usually is a null character string,and when the '+' operator is used with any character string value including a null strings, JavaScript assumes you want to concatenate the strings and not add them. Using the multiplicative identity (1) , additive identity (0), or the Number constrictor will force any number stored as a character string to a number and for a null character string force the value to numeric zero.

You can use:

var policy = this.getField("num.SS.Expense.Title.Policy").value
var search = this.getField("num.SS.Expense.Title.Search").value
var tax = this.getField("num.SS.Expense.Title.StateFee").value
var cpl = this.getField("num.SS.Expense.Title.CPL").value

event.value = policy + search + cpl + tax

// added code for debugging:
console.show();
console.clear();
console.println('typeof policy: ' + typeof policy);
console.println('typeof search: ' + typeof search);
console.println('typeof tax: ' + typeof tax);
console.println('typeof cpl: ' + typeof cpl);
console.println('typeof total: ' + typeof event.value);

To see what the type of data, number or string, is being processed.

George Kaiser

David Dunn
Registered: Oct 28 2010
Posts: 116
Thank you both for your help and very thorough responses.

David D

David Dunn
Registered: Oct 28 2010
Posts: 116
Ooops, not done yet. I employed a technique from John Dubert's book that blanks a field using
if(a + b + c = 0)
event.value = ""
else
event.value = a + b + c

...to avoid unsightly zeros in fields. However, as revealed by the debugging code, doing that causes the field value to be treated as a string instead of a number, and therefore I'm right back where I started. I could write the code to hide the field if the value is 0.00, but is there another way to solve the issue that you might recommend?

David D

gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4307
The '=' operator is not the equality operator. So your code will never work as expected.

Are you getting any errors on the JS debugging console?

What result are you getting?

I would force the values for 'a', 'b', and 'c' to a number before performing any computation.

George Kaiser

gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4307
duplicate post.

George Kaiser

David Dunn
Registered: Oct 28 2010
Posts: 116
First, I apologize for 2 things: (1) I misunderstood George Johnson's suggestion; did not grasp what he was saying (working too late, I suppose). (2) In my last post before this reply, I misstated Dubert's technique for avoiding displaying zero's, which should be this:

if(a + b +c != 0)

event.value = a + b + c
else
event.value = ""

Without the unary + operator Dubert's technique results in a null character string and causes concatenation, but once I add the unary + operator, Dubert's technique works without concatenation, as revealed by George Kaiser's very helpful debugging code.

David D