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

Trying to multiply two cells, then round the result.

New Here ,
Feb 10, 2016 Feb 10, 2016

Using Acrobat Pro- Creative Cloud.


I'm setting up a fillable PDF form. I think I need a custom calculation script to accomplish my goal. I'm trying to have two fields multiply, then round the result (up or down) to two decimals. My fields are formatted for currency, at 2 decimal places.

In excel it would be the equivalent (I believe) to something like =ROUND(MULTIPLY(Line4, Line5);2). With "Line4" and "Line5" being the names of the fields I'm trying to query. In Javascript, though, I have know idea how to pull that off. I've found pieces of what I want to do in other discussions, but I don't know enough syntax to parse them together.

Any help would be greatly appreciated!

-John

[EDIT] Just realized I might need to clarify a bit. My initial simple calculation was giving my client unexpected results. In my example above, "Line4" is a currency amount (let's say $50.45). The input of "Line5" is formatted as a percentage (let's say 10%). The field I'm trying to set up would calculate 10% of $50.00. That's all fine and easy with the built in scripts to multiply fields. In practice, the resulting sum was off by .01. In trouble shooting, I expanded the field to show 3 decimal places. The result was .045. I'm trying to get that total to round to .05.

TOPICS
Acrobat SDK and JavaScript
2.3K
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
LEGEND ,
Feb 10, 2016 Feb 10, 2016

Try something like:

// Custom calculation script

(function () {

    // Get the field values, as numbers

    var v1 = +getField("Line4").value;

    var v2 = +getField("Line5").value;

    // Multiply the values

    var result = v1 * v2;

    // Set the value of this field, rounding to two decimal places

    event.value = util.printf("%.2f", result);

})();

More information in the util.printf method is in the Acrobat JavaScript reference. This could be simplified a bit, but it should be clear. If either of the input fields is blank, the result will be 0.00, but you could add additional code if you want the field to be blank.

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 10, 2016 Feb 10, 2016

That worked as expected! Thanks George!

-John

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
LEGEND ,
Feb 11, 2016 Feb 11, 2016

If the input values for the calculation are coming from a field being calculated from other fields, you may be getting the error due to an accumulated rounding error because the other fields' values are not the rounded value but the unrounded value of the calculations. You may need to force the results of your calculations to be the rounded value and not the raw values. This is especially true with tax forms or bills of sales.

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 11, 2016 Feb 11, 2016
LATEST

How would I go about forcing that rounding? I assume adding a rounding script to all the fields my final calculation field is pulling from?

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