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

Seemingly simple calculation goes wrong

407chelsie
Registered: Dec 8 2008
Posts: 68
Answered

Can anyone help me calculate a total in Acrobat 8 Professional:

Annual Enrollment Fee = $865.00
Number of Staff [ ] x $12 = [ ]
Grand Total = [ ]

I know this seems SO simple, but nothing I'm doing is working. Do I need to set up invisible fields or can the 865 and 12 be in a calculation script? Is there a validation I need to set up? What happens if the user changes the staff #, will it automatically recalculate the total?

My Product Information:
Acrobat Pro 8.1.3, Macintosh
George_Johnson
Online
Expert
Registered: Jul 6 2008
Posts: 1876
This might help: http://www.acrobatusers.com/tech_corners/javascript_corner/tips/2006/form_calculations/

You don't have to set up hidden fields, but you can if you want to use one of the built-in calculation methods (e.g., value is the sum..., simplified field notation). If you want to use JavaScript, you would simply use the constants 865 and 12 in your script. But it's not clear to me what formula your script should use. If you describe in words what the grand total should be, someone could suggest specific code. Is it?: Grand Total = $865 + Number of Staff x $12

If so, the custom calculation script for the grand total field could be:

// Get number of staffvar num_staff = getField("Number of Staff").value; // calculate grand total and set this field's value to the resultevent.value = 865 + (num_staff * 12);

You would replace "Number of Staff" in the code above with the actual name of your field. You might want to add code to prevent the display of a result if the number of staff is blank (or zero):

// Get number of staff, as a numbervar num_staff = +getField("Number of Staff").value; if (num_staff !== 0) {// calculate grand total and set this field's value to the resultevent.value = 865 + (num_staff * 12);} else {// Blank this fieldevent.value = "";}

And yes, the calculation script will automatically recalculate the grand total whenever any field value changes.

Regarding validations, you may want to set up a validation to prevent the user from entering an invalid number (e.g., number of staff = -1, 1,000,000, or 2.7).

George
407chelsie
Registered: Dec 8 2008
Posts: 68
I placed your scripts in the grand total field, but I'm missing the calculation script that gives me the (Number of Staff x $12). What would go there?
George_Johnson
Online
Expert
Registered: Jul 6 2008
Posts: 1876
It would be very similar, something like:

// Get number of staff, as a numbervar num_staff = +getField("Number of Staff").value; if (num_staff !== 0) {// calculate total and set this field's value to the resultevent.value = num_staff * 12;} else {// Blank this fieldevent.value = "";}

For the Grand Total field, just use one of the scripts, not both.
gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4307
You can do this all in the grand total field:

// Blank this fieldevent.value = '';// Blank staff cost fieldthis.getField("Staff Cost").value = ''; // Get number of staff, as a numbervar num_staff = +getField("Number of Staff").value; // compute totals only if there is staff involvedif (num_staff != 0) {// calculate staff cost and fill fieldthis.getField("Staff Cost").value =  num_staff * 12;// calculate grand total and set this field's value to the resultevent.value = 865 + this.getField("Staff Cost").value;}

Or each field individually, but you may need to fix the field calculation order:

// staff cost calculation// Blank this fieldevent.value = ''; // Get number of staff, as a numbervar num_staff = +getField("Number of Staff").value; // compute totals only if there is staff involvedif (num_staff != 0) {// calculate staff cost and fill fieldevent.value =  num_staff * 12;}

// grand total calculation// Blank this fieldevent.value = ''; // Get number of staff, as a numbervar num_staff = +getField("Number of Staff").value; // compute totals only if there is staff involvedif (num_staff != 0) {event.value = 865 + this.getField("Staff Cost").value;}

George Kaiser

407chelsie
Registered: Dec 8 2008
Posts: 68
I got all confused with the latest answers to my question. So here's what I did.

I used George_Johnson's original calculation and validation scripts for the Grand Total field.

In the field that shows the total of (number of staff x $12) I placed the following calculation script:

// Get number of staff
var num_staff = getField("Number of Staff").value;

// calculate grand total and set this field's value to the result
event.value = num_staff * 12;

And the following validation script:

// Get number of staff, as a number
var num_staff = +getField("Number of Staff").value;

if (num_staff !== 0) {
// calculate grand total and set this field's value to the result
event.value = num_staff * 12;
} else {
// Blank this field
event.value = "";
}

I'm sure I seriously butchered the code but it's working beautifully. Do any or you foresee a problem with what I did? If not, I'm golden!
George_Johnson
Online
Expert
Registered: Jul 6 2008
Posts: 1876
There should not be a validation script for that field, so get rid of that script. You can use either the first or the second as the calculation script for the field. The second simply blanks the field if it is zero.

George
407chelsie
Registered: Dec 8 2008
Posts: 68
If I completely remove the validation script I get $0.00 in the Staff Total.
George_Johnson
Online
Expert
Registered: Jul 6 2008
Posts: 1876
If you want to supress the display of the zero value, then the calculation script for the field should be:

// Get number of staff, as a numbervar num_staff = +getField("Number of Staff").value; if (num_staff !== 0) {// calculate total and set this field's value to the resultevent.value = num_staff * 12;} else {// Blank this fieldevent.value = "";}

It's that last part that prevents zero from being displayed. I probably should not have shown the two possible scripts as I think it confused things.
407chelsie
Registered: Dec 8 2008
Posts: 68
You should be on my Christmas list. Thank You!
rllundy
Registered: Mar 12 2009
Posts: 5
I am trying to surpress the diplay of zeros from a calclation in a form that I created with Adobe Pro 9. I tried to use the following in my field where the calculation occurs as a simple calulation: Where BusinessC_1 is the field name that the calculation is done and TotalA_1 & CommutingB_1 are the field names that has data.// Get BusinessC_1, as a number
var num_staff = +getField("BusinessC_1").value;

if (BusinessC_1 !== 0) {
// calculate grand total and set this field's value to the result
(TotalA_1)-(CommutingB_1);
} else {
// Blank this field
event.value = "";
}

Thanks
gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4307
Have you looked at the error messages?

You can not use field properties, field methods, logical branching, loops, etc in the "Simplified field calculation". You can only use simple field names, constants and the 4 basic arithmetic oeprators.

If you are going to use the simplified field notation, you can not use this action to perform a calculation and supress the zero result. But, you can use the "Custom Validation script:" on the "Validation" tab to supress the zero result.

George Kaiser

rllundy
Registered: Mar 12 2009
Posts: 5
Can you tell me how I would do that?
gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4307
First get your calculation to work!

Carefully read the error messages that are being produced in the JavaScript debugging console, [url=http://www.acrobatusers.com/tutorials/2006/javascript_console]The Acrobat JavaScript Console (Your best friend for developing Acrobat JavaScript)[/url].

You can use the following script to supress a zeor result by any method of calculation, but it will not work with a field formatted with a monetary symbol:
if(event.value == 0) event.value = '';

George Kaiser

rowelc
Registered: Nov 11 2008
Posts: 5
I am using Acrobat 9 Pro for Macintosh. I created a pdf out of InDesign. I'm trying to make it an interactive/fillable pdf.

Field X
Field Y
Field Z

Field Z would be the result of Field Y being divided by Field X.
All three fields are formatted as a number, decimal places “2,” no currency symbol. Field values are not validated. For Field [Z] in the calculation, I checked “Simplified Field Notation” and entered: Y / X

Field [Z] would be a result of division [Y divided by X]. It calculates correctly, but pop up messages keep appearing all over the document when you try to enter information in other fields that say: The value entered does not match the format of the field [Z]. And then it pops you back to the page with that field on it [Z].

HELP! How can I rectify this problem? Need some answers soon.
gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4307
Please read the responses to your previous posts.

You can not divide by zero!

George Kaiser