6 Replies Latest reply on Jul 26, 2011 4:55 PM by JBinReno

# Desperately need help!

I have no experience whatsoever with JavaScript (or writing any other kind of scripts for that matter). However, I am trying to create a PDF form in which I need a field to calculate the percentage of the two prior fields, and the only way I can see to do it is to write a custom calculation script. Can anyone help me with this task? I would greatly appreciate any help you can offer, even if it's telling me I'm in the wrong place and where the more appropriate forum would be. Thanks in advance!

• ###### 1. Re: Desperately need help!

If you provide more details about the calculation you want to perform, someone should be able to help. Exactly what do you mean by "the percentage of the two prior fields"?

• ###### 2. Re: Desperately need help!

Hi George,

Thanks so much for responding. I apologize for not providing enough info. What I have is a monthly as well as fiscal year-to-date report that I have to complete. The field I'm having difficulty with is one which needs to calculate the percentage of the two previous fields. For example, field #1 has an entry of the numeral 5 (the total number of a particular type of crime committed that month), and field #2 has an entry of the numeral 3 (the total number of that type of crime that was solved or cleared that month). So I need the third field to calculate the percentage of crimes that were solved/cleared that month, i.e. I need it to do the calculation of 3 divided by 5 to get 60%. Does that make more sense?

• ###### 3. Re: Desperately need help!

Yes, that makes sense. You can use a script like the following as the custom Calculate script for the percentage field:

```// Custom Calculate script for text field
(function () {

// Get the two field values, as strings
var v1, s1 = getField("text1").valueAsString;
var v2, s2 = getField("text2").valueAsString;

// Only attempt the calculation if both fields have an entry
if (s1 && s2) {

// Convert the values to numbers
v1 = +s1;
v2 = +s2;

// Only perform the calculation of the denominator is not zero
if (v2 !== 0) {
event.value = v1 / v2;
} else {
event.value = "";
}

} else {
event.value = "";
}

})();
```

Replace "text1" and "text2" with the actual names of the input fields you're using. You can set up the field with a Format type of Percentage, or you could revise the code to output the value as you'd like to see it. It blanks the field if either input field is blank or if the second field evaluates to zero.

If you use a percentage format type, it will show 0.00% when the code sets its value to blank. You can get it to show as blank by using a custom Format script.

• ###### 4. Re: Desperately need help!

Note that text2 corresponds to the total number input field and text1 corresponds to the number cleared field.

• ###### 5. Re: Desperately need help!

George,

You are a LIFE SAVER!! I so much appreciate you taking the time to type out the script for me. I'm going to copy and paste it into the custom calculation script field and replace the appropriate words and pray it works! I've seen posts from other people alluding to a free online guide to JavaScript which can teach someone like me how to write these scripts myself, but no one has said precisely where I can find it. Would you happen to know, or can you send me wherever else you feel would be a beneficial place for me to learn about JavaScript, please?

Thanks again. I can't tell you how much I appreciate your time!

• ###### 6. Re: Desperately need help!

George,

If I could hug you through my computer, I would! The script works PERFECTLY!! Thank you so much!