Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Exit

0

/t5/acrobat-sdk-discussions/sum-two-fields-then-divide-by-2/td-p/8404275
Jul 06, 2016
Jul 06, 2016

Copy link to clipboard

Copied

(JJPC MCKTOTAL1 + JDPC MCKTOTAL1)/2

Can someone provide a Script for me to evaluate?

Thank you

TOPICS

Acrobat SDK and JavaScript
,
Windows

Community guidelines

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

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;

16
Replies
16

/t5/acrobat-sdk-discussions/sum-two-fields-then-divide-by-2/m-p/8404276#M52099
Jul 07, 2016
Jul 07, 2016

Copy link to clipboard

Copied

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?

Community guidelines

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

/t5/acrobat-sdk-discussions/sum-two-fields-then-divide-by-2/m-p/8404277#M52100
Jul 07, 2016
Jul 07, 2016

Copy link to clipboard

Copied

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

JJPC\ MCKTOTAL1

Community guidelines

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

Monteigo99
AUTHOR

Explorer
,

/t5/acrobat-sdk-discussions/sum-two-fields-then-divide-by-2/m-p/8404278#M52101
Jul 07, 2016
Jul 07, 2016

Copy link to clipboard

Copied

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.

Community guidelines

/t5/acrobat-sdk-discussions/sum-two-fields-then-divide-by-2/m-p/8404279#M52102
Jul 07, 2016
Jul 07, 2016

Copy link to clipboard

Copied

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;

Community guidelines

Monteigo99
AUTHOR

Explorer
,

/t5/acrobat-sdk-discussions/sum-two-fields-then-divide-by-2/m-p/8404281#M52104
Jul 07, 2016
Jul 07, 2016

Copy link to clipboard

Copied

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;

Community guidelines

Monteigo99
AUTHOR

Explorer
,

/t5/acrobat-sdk-discussions/sum-two-fields-then-divide-by-2/m-p/8404282#M52105
Jul 13, 2016
Jul 13, 2016

Copy link to clipboard

Copied

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);

Community guidelines

/t5/acrobat-sdk-discussions/sum-two-fields-then-divide-by-2/m-p/8404283#M52106
Jul 14, 2016
Jul 14, 2016

Copy link to clipboard

Copied

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

Community guidelines

Monteigo99
AUTHOR

Explorer
,

/t5/acrobat-sdk-discussions/sum-two-fields-then-divide-by-2/m-p/8404284#M52107
Jul 14, 2016
Jul 14, 2016

Copy link to clipboard

Copied

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

Is that what you're asking?

Thanks

Community guidelines

Monteigo99
AUTHOR

Explorer
,

/t5/acrobat-sdk-discussions/sum-two-fields-then-divide-by-2/m-p/8404285#M52108
Jul 14, 2016
Jul 14, 2016

Copy link to clipboard

Copied

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;}

Community guidelines

/t5/acrobat-sdk-discussions/sum-two-fields-then-divide-by-2/m-p/8404286#M52109
Jul 14, 2016
Jul 14, 2016

Copy link to clipboard

Copied

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

Community guidelines

Monteigo99
AUTHOR

Explorer
,

/t5/acrobat-sdk-discussions/sum-two-fields-then-divide-by-2/m-p/8404287#M52110
Jul 14, 2016
Jul 14, 2016

Copy link to clipboard

Copied

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

Community guidelines

/t5/acrobat-sdk-discussions/sum-two-fields-then-divide-by-2/m-p/8404280#M52103
Jul 07, 2016
Jul 07, 2016

Copy link to clipboard

Copied

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

Community guidelines

/t5/acrobat-sdk-discussions/sum-two-fields-then-divide-by-2/m-p/13498809#M90784
Jan 17, 2023
Jan 17, 2023

Copy link to clipboard

Copied

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

Community guidelines

/t5/acrobat-sdk-discussions/sum-two-fields-then-divide-by-2/m-p/13498821#M90785
Jan 17, 2023
Jan 17, 2023

Copy link to clipboard

Copied

Change this line:

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

To:

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

Community guidelines

/t5/acrobat-sdk-discussions/sum-two-fields-then-divide-by-2/m-p/13499429#M90788
Jan 17, 2023
Jan 17, 2023

Copy link to clipboard

Copied

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

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

Community guidelines

Community Expert
,

LATEST
/t5/acrobat-sdk-discussions/sum-two-fields-then-divide-by-2/m-p/13505093#M90806
Jan 19, 2023
Jan 19, 2023

Copy link to clipboard

Copied

No script required, you can use the simplified notation:

**(JJPC\ MCKTOTAL1 + JDPC\ MCKTOTAL1) * 0.5**

*The backslash is used to escape the space character.*

Community guidelines

Copyright Â© 2024 Adobe. All rights reserved.