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

Adding text instead of numbers

paulaeddy
Registered: May 14 2010
Posts: 22
Answered

I have the following custom calculation script. Unfortunately, it is adding the text instead of numbers. What can I do? Thanks!

event.value = this.getField("2071").value + this.getField("2073").value + this.getField("2075").value + this.getField("2077").value + this.getField("2079").value + this.getField("2081").value + this.getField("2083").value + this.getField("2085").value - this.getField("2070").value - this.getField("2072").value - this.getField("2074").value - this.getField("2076").value - this.getField("2078").value - this.getField("2080").value - this.getField("2082").value - this.getField("2084").value;

// test for less than zero

if(event.value < 0)

event.value = 0; //set result to zero

My Product Information:
Acrobat Pro 9.3, Windows
gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4307
JavaScript uses the '+' operator for addition of numbers or concatenation of strings and then tries to assume if a value is a number or a string, including a number as a character string. So these 2 conditions can result in the wrong action using the wrong type of data.

Fortunately JavaScript and Adobe provide some properties, methods, or constrictors to force data to either a string or number.

To force a value to a number, you can use the "Number(sValue)" constrictor, or use either multiplication or division with the unity value to force a numeric operation, think multiply by the number 1.

Since you may use or can use the summing of values of field names you might want to consider using a function and reusing the code for the summing of values.
// can be a document level script of place in a cusotm calculation scriptfunction Sum(aNames) {/*function to sum the values for an array of field names

*/
 var nSum = 0; // variable to accumulate the sumvar sName; // name of fieldvar nValue; // value of a field // loop through the field namesfor (i = 0; i < aNames.length; i++) {sName = aNames[i]; // get field name from array of field namesnValue = this.getField(sName).value; // get the value of i element of field namesnValue = Number(nValue); // force to numbernSum += nValue; // add number to sum} //  return nSum; // pass back the sum}

// custom calculation script using the above funciton// array of field names to addvar aAddFields = new Array("2071", "2073", "2075", "2077", "2079", "2081", "2083", "2085"); // array of field names to subtractvar aSubFields = new Array("2070", "2072", "2074", "2076", "2078", "2080", "2082", "2084"); var nResult = 0; // accumulated result// compute sum of fieldsnResult += Sum(aAddFields);// subtract the sum of the fields to subtractnResult -= Sum(aSubFields);// set field valueevent.value = nResult// test for less than zeroif(nResult < 0)event.value = 0; //set result to zero

George Kaiser

paulaeddy
Registered: May 14 2010
Posts: 22
I'm afraid I'm not very good at this. I just started creating forms a couple of weeks ago. How would I apply your suggestion to what I already have? Thanks so much for your help.
gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4307
You can place all of the code in the 'Custom JavaScript calculation', but I would place the function in the Document Level JavaScript and make it available to all the form Fields.

Tutorials by Thom Parker:
[url=http://www.acrobatusers.com/tutorials/2006/where_js_goes]So where does JavaScript live in Acrobat?[/url]
[url=http://www.acrobatusers.com/tutorials/2006/document_actions]"Where is" Series, Entering Document Actions[/url]

If you named your fields to start with an alphabetic character, then you could have used the Simplified Field Notation option.

George Kaiser

paulaeddy
Registered: May 14 2010
Posts: 22
I put the second code you gave into the custom calculation script and the result was the same:

200100200075

It is still adding the text rather than the calculating the values. The field is formatted as a number. I'm not sure what is wrong.
gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4307
Have a look at the following sample:
[url=https://acrobat.com/#d=BTBoKOwsJb0KdpT-*8SZqw[/url]and carefully look at how the typeof field changes as values are entered. Try non-sequential fields.

The null value is a character string and forces the "+" operator to concatinate and not add values.

George Kaiser

gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4307
If you want to use your original calculation, you will need to multiply the value of each field by the number '1', without the quotes, before performing the addition or subtraction.
event.value = (1 * this.getField("2071").value) +(1 * this.getField("2073").value) + (1 * this.getField("2075").value) +(1 * this.getField("2077").value) + (1 * this.getField("2079").value) +(1 * this.getField("2081").value) + (1 * this.getField("2083").value) +(1 * this.getField("2085").value) - (1 * this.getField("2070").value) -(1 * this.getField("2072").value) - (1 * this.getField("2074").value) -(1 * this.getField("2076").value) - (1 * this.getField("2078").value) -(1 * this.getField("2080").value) - (1 * this.getField("2082").value) -(1 * this.getField("2084").value);

George Kaiser

paulaeddy
Registered: May 14 2010
Posts: 22
Thanks! It is now calculating instead of adding text. However, now I'm receiving the "value entered does not match the format" error. I see that I can't divide by zero, however, this is never divided by zero, but may be dividing into zero.

Example of A that will never be zero, but B may be zero: A / B = C

Here is my final script....how do I fix this? Thanks, again!

this.getField("2077").value;
var e = this.getField("2079").value;
var f = this.getField("2081").value;
var g = this.getField("2083").value;
var h = this.getField("2085").value;
var i = this.getField("2070").value;
var j = this.getField("2072").value;
var k = this.getField("2074").value;
var l = this.getField("2076").value;
var m = this.getField("2078").value;
var n = this.getField("2080").value;
var o = this.getField("2082").value;
var p = this.getField("2084").value;
event.value = (((1 * a) + (1 * b) + (1 * c) + (1 * d) + (1 * e) + (1 * f) + (1 * g) + (1 * h) - (1 * i) - (1 * j) - (1 * k) - (1 * l) - (1 * m) - (1 * n) - (1 * o) - (1 * p)) / ((1 * i) + (1 * j) + (1 * k) + (1 * l) + (1 * m) + (1 * n) + (1 * o) + (1 * p))) * 100;
// test for less than zero

if(event.value < 0)event.value = 0; //set result to zero
gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4307
Do you have any division elsewhere in the form?

If you have 'a / b', 'a' is the dividend and 'b' is the divisor. It is possible to divide by zero or have a zero divisor in JavaScript, but your result will be a text value and not a numeric value. And this is why I ask users when there is a divisor of zero, that they change the field type to 'None' so the text returned can be displayed. You will need to test the divisor for a value of zero before attempting the division, or create a variable fro the result of the division and then test the result for being 'Not a Number' by using the 'isNaN()' function of JavaScript.

You have to realize that when one edits any one field, the form calculation might not update until another filed is updated and the entire form is recalculated or the form is cleared and a field that had a number in it now has a null value, which is treated as zero by the division operation. During this recalculation all fields are updated and their scripts run until Acrobat encounters a problem and at the point of the problem Acrobat throws an error. But the error might not be in the last field edited. Sometimes it is very hard to locate the exact field. So one needs to very carefully read and decrypt the JavaScirpt console's message.

George Kaiser

jimhealy
Team
Registered: Jan 2 2006
Posts: 146
Instead of:event.value = (((1 * a) + (1 * b) + (1 * c) + (1 * d) + (1 * e) + (1 * f) + (1 * g) + (1 * h) - (1 * i) - (1 * j) - (1 * k) - (1 * l) - (1 * m) - (1 * n) - (1 * o) - (1 * p)) / ((1 * i) + (1 * j) + (1 * k) + (1 * l) + (1 * m) + (1 * n) + (1 * o) + (1 * p))) * 100;
Do:
var tot = (((1 * a) + (1 * b) + (1 * c) + (1 * d) + (1 * e) + (1 * f) + (1 * g) + (1 * h) - (1 * i) - (1 * j) - (1 * k) - (1 * l) - (1 * m) - (1 * n) - (1 * o) - (1 * p)) / ((1 * i) + (1 * j) + (1 * k) + (1 * l) + (1 * m) + (1 * n) + (1 * o) + (1 * p))) * 100;// Checks for not a number or infinityif(isNaN(tot) || tot == (1/0))event.value = 0;elseevent.value = tot;

Jim Healy
FormRouter, Inc.
Check out our FREE Advanced Acroform Toolset:
http://www.formrouter.com/tools

Jim Healy, Founder & CEO FormRouter Inc.
Chapter Leader AUG RTP NC
http://www.formrouter.com

teletranx
Registered: Jan 11 2011
Posts: 1
I'm doing a pdf form for screen printing, and theres is a field were the person needs to type a name. I need to calculate this field as the number 1 to multiply it for the price.
Does anyone know how to do this using a script?
thanks
gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4307
You only need to test to see if the field is not a null string (empty).

// cost equals non-null value times price
event.value = (this.getField('Text1').value != '') * 25.99;

George Kaiser