Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Calculate price based on fillable field

New Here ,
Feb 01, 2016 Feb 01, 2016

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.

TOPICS
Acrobat SDK and JavaScript , Windows
1.8K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Feb 01, 2016 Feb 01, 2016

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 {

        even

...
Translate
LEGEND ,
Feb 01, 2016 Feb 01, 2016

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Feb 02, 2016 Feb 02, 2016
LATEST

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!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines