2 Replies Latest reply on Feb 2, 2016 9:20 AM by johannan11294465

# Calculate price based on fillable field

Hello,

Apologies, I know this is a very basic question but I cannot figure out the correct syntax for the Custom Calculation Script needed to generate the total cost in my form (I just got Acrobat Pro today).

I am having the user fill in the QTY field and then I am trying to set the TOTAL field to calculate cost by multiplying QTY by the price of \$29. I would have thought it was simple, like writing out a formula in Excel, but it doesn't seem to work that way and I cannot find any resources on Adobe.com or online that point me in the right direction. Any assistance anyone can offer would be greatly appreciated.

• ###### 1. Re: Calculate price based on fillable field

It might be easier to use the simplified field notation option, in which case you'd enter:

29 * QTY

If you want to use a custom calculation script, it could be something like:

// Custom calculation script for a text field

(function () {

// Get the quantity value, as a number

var qty = +getField("QTY").value;

// Calculate this field's value if the quantity is greater than 0

if (qty > 0) {

event.value = util.printf("%.2f", 29 * qty);  // round to nearest cent

} else {

event.value = "";   // Blank this field

}

})();

So this option gives you more flexibility. allowing you to round and blank the field. You can also do additional checking to make sure the quantity makes sense (e.g., a positive integer within a certain range) You'll find more information in the Acrobat JavaScript reference, and here's a link to an introductory tutorial about setting up calculations in PDF forms: https://acrobatusers.com/tutorials/how-to-do-not-so-simple-form-calculations

Be sure to set any calculated fields to read-only so the user doesn't attempt to interact with them.

• ###### 2. Re: Calculate price based on fillable field

Thank you Greg, much appreciated!

I eventually figured out the Simplified option, but the script you provided is much more user friendly for pricing in terms of rounding off to two decimal points, and leaving the field blank if there is no quantity specified.

Form looks great now!