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

How to calculate a division

New Here ,
Jul 09, 2018 Jul 09, 2018

I need to do a division as calculation and I don't know how to do it. I don't know where to find the answer. If nobody answer my question I will probably use another software than Adobe DC. i cannot find my answer on Adobe site. Really bad done

TOPICS
Create PDFs
3.7K
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 ,
Jul 09, 2018 Jul 09, 2018

Assuming you're working with a form, you'll have to use JavaScript. Here's a sample custom calculate script for a text field:

// Custom calculate script

// Get the field values, as numbers

var numerator = +getField("text1").value;

var denominator = +getField("text2").value;

// Calculate and set this field value

if (denominator !== 0) {

    event.value = numerator / denominator;

} else {

    event.value = "";

}

You'd have to replace "text1" and "text2" with the actual field names, and perhaps modify this script if you're doing something other than dividing one field value by another.

Also, that last if/else block can be reduced to just the following"

// Calculate and set this field value

event.value = denominator !== 0 ? numerator / denominator : "";

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 ,
Sep 26, 2023 Sep 26, 2023

Hi - thank you for the above; can you tell me how I can get it to check if another drop-down field = Yes, then run the division calculation.  Otherwise, leave value (%) at 0.

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
Community Expert ,
Sep 27, 2023 Sep 27, 2023

Change:

if (denominator !== 0) {

To:

if (denominator !== 0 && this.getField("Dropdown1").valueAsString=="Yes") {

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 ,
Sep 28, 2023 Sep 28, 2023
Thank you! I really appreciate the help. Can you please confirm if I have
this interpreted/inserted correctly?

// Custom calculate script

// Get the field values, as numbers

var numerator = this.getField("Field Name").value;

var denominator = this.getField("Field Name").value;

// Calculate and set this field value

if (denominator !== 0 && this.getField("Dropdown1").valueAsString=="Yes")

{event.value = denominator !== 0 ? numerator / denominator : "";}

else {event.value = "";}


Have a super day!
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
Community Expert ,
Sep 28, 2023 Sep 28, 2023

Yes, but you have to replace "Field Name" and "Dropdown1" with the actual names of the fields in your file...

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 ,
Sep 28, 2023 Sep 28, 2023
LATEST
Perfect - thank you!!

I will be adding the script into the document today.

Have a super day!
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 ,
Aug 15, 2018 Aug 15, 2018

When I write the code you gave me it does not work. When I write 'this.getField' instead of +getField it works. Why?

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
Community Expert ,
Aug 15, 2018 Aug 15, 2018

The "this" keyword can sometimes be dropped when it is applied from the context, but I don't like this habit and recommend to always use it. Others disagree.

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 ,
Aug 15, 2018 Aug 15, 2018

Where exactly did you place the code?

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