Skip to main content
Participating Frequently
November 3, 2016
Question

How to Sum Percentage fields

  • November 3, 2016
  • 13 replies
  • 1255 views

Hello,

I haven't played with JavaScript in a bit and I'm a novice at that.

I have 9 fields that have a manual entry which is a percentage (I have the field set to 'NONE' format because the percentage format spits out too many zeros).

Score1=15%

Score2=10%

etc.

All fields must add up to 100% and display my TotalScore field.

I can get any of my scripts to work.

Please assist.

Thank you

This topic has been closed for replies.

13 replies

COSMontyAuthor
Participating Frequently
November 10, 2016

Ok, so I made the following corrections and am still getting this error at the 'bold' location:

SyntaxError: missing ) after argument list 1: at line 2

var dd1 = this.getField(Dropdown1.0).value;
var m1 = this.getField(Max1).value;

If dd1 == "MET-100%" {
Event.value = "m1";
} else {
if dd1 == "NOT MET-0%" {
event.value = .00;
}}

I've tried several things and it won't clear.

Inspiring
November 10, 2016

Are your field names in the "getField" method the names of the fields or variable whose value is the field's name?

var dd1 = this.getField("Dropdown1.0").value; // using field name;
var m1 = this.getField("Max1").value; // using field name;

If dd1 == "MET-100%" {
Event.value = m1; // using variable's value;
} else {
if dd1 == "NOT MET-0%" {
event.value = .00;
}

It might help to see your form or a sample form with the error issue. You will need to place the form file in a web file sharing service like dropbox, google drive, etc.

COSMontyAuthor
Participating Frequently
November 10, 2016

Unfortunately I am unable to upload my form (I work for a federal agency).

The quotes around the var field name fixed the first error. But now I get this:

SyntaxError: Missing ; before statement 4: at line 5

var dd1 = this.getField("Dropdown1.0").value;
var m1 = this.getField("Max1").value;

If dd1 == "MET-100%" {
event.value = m1;
} else {
if dd1 == "NOT MET-0%" {
event.value = .00;
}}

This is the format of the form:

Criteria                              Max Score               Met/Not Met (Drop Down Options)          Score

1. Timely completion               20%                    Met-100%                                                I want to populate this field

Inspiring
November 5, 2016

Not so simple if you want to suppress the error result when the total of the "A" column is zero and one wants to suppress the "DIV/0!" error and suppress "0" for calculated cells.

Which calculation option did you use?

Have you looked at the JavaScript console for any errors, key combination <CTRL> + "J"?

Are you getting any pop-up errors?

I use the "Custom JavaScript". Just like in Excel division by zero causes an error condition the same happens with calculations in PDF forms, so one needs to adjust the script for this condition.

Portfolio Compute Percentages

has working PDF forms without errors and with errors along with an Excel file

COSMontyAuthor
Participating Frequently
November 4, 2016

Alright! So I decided to approach it from a different direction. Instead I used a Dropdown list and then tried to use a script to grab the value selected and give either a '.##' or reference another cell with the actual number.  Noting is working and I've tried several.  Once I get this script to work, I can then just use the simple 'Value is the...Sum' option.

Here is one version.

var dd1 = this.getField("Dropdown1.0").value;

var max1 = this.getField("Max%").value;

if (dd1 = “MET-100%”); {

event.value = (".2");

} else {

if (dd1 = “NOT MET-0%”); {

event.value = ".00";

}

Instead of this//

event.value = (".2");

I used this//

event.value = ("max1");

try67
Community Expert
Community Expert
November 5, 2016

There are several issues with your code:

1. The comparison operator in JS is "==" (or "==="), not "=". That is the value assignment operator.

2. You must only use "straight" single or double-quotes in your code. Not this kind of quotes: “NOT MET-0%” ...

3. If you want to refer to a variable don't put quotes around its name. Otherwise it's just a text string.

Inspiring
November 3, 2016

What is the latest script you tried?

Inspiring
November 3, 2016

If you are going to add a set fields by either the "the field is the sum of the following fields" or the "simplified field notation" the fields in question must have a "Number" or "Percent" format not the "None" or any other option.

You should be aware that the "Format" of a field is the displayed value only. So if a number field has a currency symbol that symbol is only present in the displayed value and the not the actual value of the field. For percentages the actual value of the field is multiplied by 100 and has the "%" post fixed to the displayed value. This lets one use the decimal value of the percentage for mathematical calculations without any addition changes to the fields actual value. If you sum a series of percentage fields that add up to 100% the actual sum of the fields will be 1.00.

If you want to suppress the zeros when using the Percentage format one needs to use scripting to hide the field or dynamically set the format of the field to the Number format or Percentage format as necessary. How to use the special built-in functions for dynamically setting the format for fields was firs documented in the Acrobat JavaScript Reference API and noted in version 6 and 7 documentation.

If you do not use the "Field is the ..." or the "Simplified Field Notation" methods, you need to be careful for field with a null value since for some actions the null value is treated as a zero number and others it is treated as a null string and in JavaScript the "+" operator is the additional operator and the concatenation operator depending upon how JavaScript assume the value to be a number of a string. You need to force the empty or null values to be a the number zero.