These forums are now Read Only. If you have an Acrobat question, ask questions and get help from one of our experts.

calculation and null fields

ochsnermr
Registered: Oct 6 2010
Posts: 4
Answered

I have read through the forums and tried to copy several scripts to no avail. Here is the 1st scenario: I want a field to calculate a students Attendance percentage based on the days present and days absent. So the formula on paper is Days Present / (Days present + Days absent). My fields are named DP, DA and Attendance
 
Here is the 2nd scenario: I also have another field that will display a simple division formula A/ P but I do not want any of these fields to return the message " The value entered does not match the format of the field []" so I know I also need my scripts to indicate that there can be a null value or zero in any of these 6 fields.
 
All help is greatly appreciated!
 
*I am using 9.3.3 Pro

My Product Information:
Acrobat Pro 9.3, Windows
try67
Expert
Registered: Oct 30 2008
Posts: 2398
Custom calculation script for the 1st scenario:

DA = this.getField("DA").value;
DP = this.getField("DP").value;
// First we make sure all values are filled in
if (DA==null || DA=="" || DP==null || DP=="") {
event.value = "";
} else {
// Now we make sure the sum of the days present and days absent is not zero, to prevent division by zero
totalDays = DA + DP;
if (totalDays==0) {
event.value = "";
} else {
event.value = DP / (totalDays);
}
}

Custom calculation script for the 2nd scenario:

P = this.getField("P").value;
A = this.getField("A").value
if (A == null || A == "" || P == null || P == "" || P == 0) {
event.value = "";
} else event.value = A/P;

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com

try67
Expert
Registered: Oct 30 2008
Posts: 2398
Custom calculation script for the 1st scenario:

DA = this.getField("DA").value;
DP = this.getField("DP").value;
// First we make sure all values are filled in
if (DA==null || DA=="" || DP==null || DP=="") {
event.value = "";
} else {
// Now we make sure the sum of the days present and days absent is not zero, to prevent division by zero
totalDays = DA + DP;
if (totalDays==0) {
event.value = "";
} else {
event.value = DP / (totalDays);
}
}

Custom calculation script for the 2nd scenario:

P = this.getField("P").value;
A = this.getField("A").value
if (A == null || A == "" || P == null || P == "" || P == 0) {
event.value = "";
} else event.value = A/P;

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com

try67
Expert
Registered: Oct 30 2008
Posts: 2398
Custom calculation script for the 1st scenario:

DA = this.getField("DA").value;
DP = this.getField("DP").value;
// First we make sure all values are filled in
if (DA==null || DA=="" || DP==null || DP=="") {
event.value = "";
} else {
// Now we make sure the sum of the days present and days absent is not zero, to prevent division by zero
totalDays = DA + DP;
if (totalDays==0) {
event.value = "";
} else {
event.value = DP / (totalDays);
}
}

Custom calculation script for the 1st scenario:

P = this.getField("P").value;
A = this.getField("A").value
if (A == null || A == "" || P == null || P == "" || P == 0) {
event.value = "";
} else event.value = A/P;

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com

try67
Expert
Registered: Oct 30 2008
Posts: 2398
Custom calculation script for the 1st scenario:

DA = this.getField("DA").value;
DP = this.getField("DP").value;
// First we make sure all values are filled in
if (DA==null || DA=="" || DP==null || DP=="") {
event.value = "";
} else {
// Now we make sure the sum of the days present and days absent is not zero, to prevent division by zero
totalDays = DA + DP;
if (totalDays==0) {
event.value = "";
} else {
event.value = DP / (totalDays);
}
}

Custom calculation script for the 2nd scenario:

P = this.getField("P").value;
A = this.getField("A").value
if (A == null || A == "" || P == null || P == "" || P == 0) {
event.value = "";
} else event.value = A/P;

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com

try67
Expert
Registered: Oct 30 2008
Posts: 2398
Sorry for the double-posts.... I kept trying to post it, but the site kept getting stuck.
All but the last one can be removed.

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com

gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4307
For any of the three calculation options you can use the "Validation" tab's "Custom validation script" to convert a zero value to a null.

if(event.value == 0) event.value = "";


George Kaiser

ochsnermr
Registered: Oct 6 2010
Posts: 4
Thank you so much!!! You are truly a life saver =D
ochsnermr
Registered: Oct 6 2010
Posts: 4
New problem. The total days formula seemed to work but it doesn't calculate correctly if the student was absent 0 days so that the attendance percentage becomes 100% What do I need to change in the formula so that i get 100% when the student is absent 0 days?

Thanls in Advance!
gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4307
I think the calculation is more complex than necessary. A null string when considered a number is zero, so one could use the 'Number()' constrictor to force a null value to zero.

// begin code
// calculate days present percentage
// get number of days absent
DA = Number(this.getField("DA").value);
// get number of days present
DP = Number(this.getField("DP").value);
// compute total number of days
totalDays = DA + DP;
if (totalDays == 0) {
// no days present
event.value = "";
} else {
// compute percentage
event.value = DP / totalDays;
}
// end code

George Kaiser

ochsnermr
Registered: Oct 6 2010
Posts: 4
yes that is what I needed; thank you George!