JavaScript custom calculation script for a percentage
Copy link to clipboard
Copied
I have created a fillable PDF form in Acrobat Pro DC that needs to tally the scores (on a scale of 1-5) given in several text fields (I used radio buttons), divide it by the total score possible (25) and display it at the end of the form as a percentage. I was told I need a JavaScript custom calculation script. Originally I tried using a simplified field notation, but that did not work. Can anyone help convert this formula to a custom calculation script: ((sum1+sum2+sum3+sum4+sum5)/25)*100.
Thank you for your time.
Copy link to clipboard
Copied
You can use this code:
var sum = Number(this.getField("sum1").value) + Number(this.getField("sum2").value) + Number(this.getField("sum3").value) +
Number(this.getField("sum4").value) + Number(this.getField("sum5").value);
event.value = (sum/25)*100;
Copy link to clipboard
Copied
PS. If you set the field as having a Percentage formatting then you shouldn't multiply by 100. The value needs to be between 0 (0%) and 1 (100%).
Copy link to clipboard
Copied
try67 Thank you so much for providing this solution. I still have not got it working correctly, but I'm closer than where I was before.
-- I set the field Format as Percentage (previously I had missed this step).
-- I deleted *100 from the end of the formula in the custom calculation script.
-- Now when I test the form, the calculation is showing as 0.00%. I just cannot see why it will only come out to 0.00% instead of a true calculation.
I appreciate your time, thank you again.

Copy link to clipboard
Copied
You need to change the value of one of the fields involved for it to kick-in.
Also, check the JS Console (Ctrl+J) for any error messages.
If you've done all of that and it's still not working then you'll need to share the actual file with us.