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

Sum two fields then Divide by 2

Explorer ,
Jul 06, 2016 Jul 06, 2016

(JJPC MCKTOTAL1 + JDPC MCKTOTAL1)/2

Can someone provide a Script for me to evaluate?

Thank you

TOPICS
Acrobat SDK and JavaScript , Windows
2.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

correct answers 1 Correct answer

Community Expert , Jul 07, 2016 Jul 07, 2016

The basic structure of the script would be:

var v1 = Number(this.getField("FieldName1").valueAsString);

var v2 = Number(this.getField("FieldName2").valueAsString);

event.value = (v1+v2)/2;

Translate
LEGEND ,
Jul 07, 2016 Jul 07, 2016

You could put your calculation into the "Simplified Field Notation" method option.

If I remember correctly adding a number to itself and dividing by 2 will result in the number.

Have you looks at any of the free tutorials or videos on form creation?

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 ,
Jul 07, 2016 Jul 07, 2016

If the field names contain spaces, though, you'll need to escape them, like this:

JJPC\ MCKTOTAL1

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
Explorer ,
Jul 07, 2016 Jul 07, 2016

Thank you. The two fields are different; they have different names. I can't use the simplified field because it doesn't have secondary functions; e.g. it will add ONLY, but will not then divide.

I'm an advanced form creator, except when it comes to JavaScript.

I've searched all over the internet for this script, but all I get is simple tutorials that do not provide the structure of the entire syntax with all the {,},; etc. I don't know how to structure the script properly.

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 ,
Jul 07, 2016 Jul 07, 2016

The basic structure of the script would be:

var v1 = Number(this.getField("FieldName1").valueAsString);

var v2 = Number(this.getField("FieldName2").valueAsString);

event.value = (v1+v2)/2;

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
Explorer ,
Jul 07, 2016 Jul 07, 2016

It worked! Thank you.

var v1 = Number(this.getField("JJPC_MCKTOTAL1").valueAsString); 

var v2 = Number(this.getField("JDPC_MCKTOTAL1").valueAsString); 

event.value = (v1+v2)/2;

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
Explorer ,
Jul 13, 2016 Jul 13, 2016

So, the above script works but I just figured out that I need the event.value to only calculate if v2 is >0.  I tried this but it didn't work. Any assistance is appreciated.

var v1 =
Number(this.getField("JJPC_MCKTOTAL1").valueAsString); 

var v2 =
Number(this.getField("JDPC_MCKTOTAL1").valueAsString); 

event.value = If((v2>0),(v1+v2)/2,v1);

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 ,
Jul 14, 2016 Jul 14, 2016

What should be the value of the field if that condition is not true?

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
Explorer ,
Jul 14, 2016 Jul 14, 2016

If V2>0, then add v1+v2, if it is false, then v1.

Is that what you're asking?

Thanks

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
Explorer ,
Jul 14, 2016 Jul 14, 2016

I've derived this formula that works for 5 of 6 fields. On the 6th field it is a one adjustment calculation delayed (I have 5 Y/N drop down list options that get calculated in a field, one for the first 6mths, then on another field for the second 6 mths).

What's wrong with this?

var v1 =
Number(this.getField("JJNotesCompletionRate").valueAsString); 

var v2 =
Number(this.getField("JDNotesCompletionRate").valueAsString); 

if(v2>0) {

event.value = (v1+v2)/2;

} else {

event.value = v1;}

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 ,
Jul 14, 2016 Jul 14, 2016

Thr code is fine. The issue is with the Fields Calculation Order.

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
Explorer ,
Jul 14, 2016 Jul 14, 2016

Yep, that's what I found. It works now.  Thank you

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 07, 2016 Jul 07, 2016

Not true. If you divide by a constant non-zero value it should work.

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 ,
Jan 17, 2023 Jan 17, 2023

How would you get the calculation to round to the nearest hundreth? 

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 ,
Jan 17, 2023 Jan 17, 2023

Change this line:

event.value = (v1+v2)/2;

To:

event.value = ((v1+v2)/2).toFixed(2);

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 ,
Jan 17, 2023 Jan 17, 2023

To round to the nearest hundredth, you need to use like this:

event.value = Math.round(((v1+v2)/2)/100)*100;

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 ,
Jan 19, 2023 Jan 19, 2023
LATEST

No script required, you can use the simplified notation:

(JJPC\ MCKTOTAL1 + JDPC\ MCKTOTAL1) * 0.5

 

The backslash is used to escape the space character.

 

Capture_2301200044.png


Acrobate du PDF, InDesigner et Photoshopographe
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