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

String Instead of Sum of Numbers

AlanY
Registered: Jan 21 2011
Posts: 2
Answered

Help! Instead of adding the numbers, I get a string, i.e., 15 + 1 = 151 instead of 16. I have three rows with three columns. Basically A X B = C, D X E = F, and G X H = I where numbers are entered only in the A, D, and G fields. Then on the fourth row I want a total of C, F, and I.
 
I don't have a problem when A, D, and G are entered with a number or when A and D contain a number.
 
However, when D is blank or zero and I try to add C and I, the total is not a sum of the numbers but a string, i.e., 15 plus 1 becomes "151".
 
I'm also trying to keep the fields blank if no numbers are entered in A, D, or G.
 
Any ideas? Thanks.
 
My calculation scripts:
 
Row 1.
var a = this.getField("daysThisYear");
var b = this.getField("ratio1");
var c = this.getField("totalThisYear");
var d = 0;
d = (a.value * b.value);
if (d>0) {c.value = d;}
else {if (d<=0) {c.value = ("")}}
 
Row2.
var a = this.getField("daysLastYear");
var b = this.getField("ratio2");
var c = this.getField("totalLastYear");
var d = 0;
d = (a.value * b.value);
if (d>0) {c.value = d;}
else {if (d<=0) {c.value = ("")}}
 
Row3.
var a = this.getField("days2YearsAgo");
var b = this.getField("ratio3");
var c = this.getField("total2YearsAgo");
var d = 0;
d = (a.value * b.value);
if (d>0) {c.value = d;}
else {if (d<=0) {c.value = ("")}}
 
Row4 Total field.
var a = this.getField("totalThisYear");
var b = this.getField("totalLastYear");
var c = this.getField("total2YearsAgo");
var d = this.getField("totalAllYears");
var e = 0;
e = (a.value + b.value + c.value);
if (e>0) {d.value = e;}
else {if (e<=0) {d.value = ("")}}
 
 

My Product Information:
Acrobat Pro Extended 9.4, Windows
George_Johnson
Expert
Registered: Jul 6 2008
Posts: 1876
Accepted Answer
You can explicitly convert the field value to a number using the unary + operator:

e = (+a.value + +b.value + +c.value);


A blank field value will get converted to zero (0), so it will result in numerical addition as you want.
gkaiseril
Online
Expert
Registered: Feb 23 2006
Posts: 4308
The '+' operator is the addition operator or the concatenation operator. JavaScript looks at the values and add numbers or concatenates character strings or character strings and numbers. JavaScirpt is not very good at detecting character strings, so one needs to use a constrictor, method, function or operation to force a value to a string or number. Multiplication by 1, the multiplication identity, will force a null string to a number, zero. You can also use the 'Number' constrictor to force a number. For string there is the 'String' constrictor and the 'toString()' method.

George Kaiser

AlanY
Registered: Jan 21 2011
Posts: 2
Wow! George J answered in less than 20 minutes! Your solution worked. And thanks to George K for his explanations. I just couldn't get them to work. Thanks again, Alan Y