Skip to main content
Participating Frequently
November 29, 2021
Question

Seeking Help with Adobe Forms Calculations - Specifically with rounding answers and omitting blanks

  • November 29, 2021
  • 1 reply
  • 666 views

Hello,

I am preparing a form with calculations and need assistance on a couple of areas.  I am very new to javascript calculations and am a bit confused by some of the abbreviations I'm seeing when trying to look through other discussions on this board.  Maybe someone can help give me the exact code I should be using and the box I should enter it into?

 

 1. In several locations, I have the user enter data in a specific column.  The data cells are all formatted as numbers and the final cell at the bottom sums them up.  That works great, but when I change the "sum" to "average" I get fractional numbers.  I want this form to work only in wole numbers and need the "average" to not only display as a whole number (ie Format tab) but also to round up for subsequent calculations.  How do I enter a "Custom Calculation Script" in the "Calculate" tab so that my "average" cell is a whole number?

 

2. User's are not required to enter data on every row.  The "average" calculation in the "calculate " tab includes all cells that I've selected to be included in the calculation but does not omit empty cells from the calculation and the result is incorrect.  How do I omit empty cells from the "average" calculation?

 

Thank you!!

 

 

This topic has been closed for replies.

1 reply

Participating Frequently
November 30, 2021

I have found the following ocde and tried several times to get it to work, but to no avail.  I just get a blank text box.  What am I doing wrong?

 

Here's the code I'm trying to use to get the average.  I haven't tried to add code yet to round the answer - though I need to.

 

//Initialize variables
var i, v, num = 0, sum = 0;

// Loop through the input fields
for (i=1; i<=7; i++) {
v = +getField("table2_"+i).value;
if (v !== 0) {

// increment the non-blank/zero field count
num++;

// add the field value to the running total
sum +=+f.value;
}
}

// Calculate the average
if (num) {
event.value=sum/num;
} else {

// ALL fields are empty, so set to blank
event.value = "";
}

 

I have a total of 15 rows (though I've been experimenting with just 7 rows since I'm learning) and I've named them all "table2_row 1","table2_row2","table2_row3" etc to row 7 (I'll make it 15 when I get the formula right).

 

I'm entering the code into the "custom calculation script" on the "calculate" tab of the "Text Field Properties" box for the filed I'm trying to populate.  Is that the right place?  Should I be entering this under the Javascript tool?  I have multiple sections on this form that require separate calculations so I don't want to get them confused.

 

Anyone know how to help?

Bernd Alheit
Community Expert
Community Expert
November 30, 2021

Check the Javascript console for errors.

Participating Frequently
November 30, 2021

I don't get a syntax error but do get a "Warning: JavaScript Window - " which tells me that "The value entered does not match the format of the field [text9]".

 

I've tried reformatting the text9 box but not sure what I'm missing as it's already set to a number.