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

Average of text boxes with values

Community Beginner ,
Mar 08, 2024 Mar 08, 2024

Hi,

I'm trying to write a custom calculation script for a text box that calculates the average value of the numbers entered into several other text boxes, but also omits from the denominator any text boxes that don't have a value.  For simplicity sake, I named the text boxes that will contain the numbers going into the average calculation A1, A2, etc. through A11.  So for example if 9 of the text boxes have values, it would be the sum of values divided by 9 instead of 11.  I looked at a couple of code examples on this board and couldn't get them to work correctly.  Could anyone show me how to calculate this average?

 

Thank you.

TOPICS
PDF , PDF forms
833
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
1 ACCEPTED SOLUTION
Community Expert ,
Mar 08, 2024 Mar 08, 2024

Search the forum for calcAverage. It's a function I wrote a while ago that does just that.

View solution in original post

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 ,
Mar 08, 2024 Mar 08, 2024

Search the forum for calcAverage. It's a function I wrote a while ago that does just that.

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 Beginner ,
Mar 08, 2024 Mar 08, 2024

Thank you!  I found it and I'm good to go!  Appreciate the help.

Peter

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 ,
Apr 28, 2024 Apr 28, 2024

Where is the information that you wrote?  I have the same issue.  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
Community Beginner ,
May 03, 2024 May 03, 2024

Hi,

This is what I ended up with - the "AB" values are the numbers from the text boxes that I wanted averaged.

 

// Custom calculation script for Adobe Acrobat
// Calculate average of six numerical values, ignoring blanks and zeros

var values = new Array(
getField("AB1").value,
getField("AB2").value,
getField("AB3").value,
getField("AB4").value,
getField("AB5").value,
getField("AB6").value,
getField("AB7").value,
getField("AB8").value,
getField("AB9").value,
getField("AB10").value,
getField("AB11").value
);

var sum = 0;
var count = 0;

for (var i = 0; i < values.length; i++) {
var value = parseFloat(values[i]);

if (!isNaN(value) && value !== 0) {
sum += value;
count++;
}
}

var average = count === 0 ? 0 : sum / count;

event.value = average;

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 ,
May 10, 2024 May 10, 2024
LATEST

Thank you and where would I put this?  in one of these??  I've attached my document as well..... Thank you Peter

Screen Shot 2024-05-10 at 10.12.04 AM.png

 Also, do you know if I can do if statements?  I'd like to put a letter grade by the class (in a drop down) and be able to have the GPA in the top cell. Also, there may not be 7 courses (some students take 6) and would need the Freshman Fall GPA cell only calculate the grades that are placed in the cell.  Thanks.. Ruth

If 

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