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

Script to tell a Form Field to Calculate Difference as a Percentage.

knowgood
Registered: Aug 20 2008
Posts: 13
Answered

Hello,

I'm in a pickle. I have a deadline for a form that needs some Javascript, but I've no idea how to achieve the desired result.

I need for a person to put in a value for 2008, then one for 2007, have Acrobat calculate the change (difference) between the two values as a positive or negative percentage with no decimals, and have that new value appear in the third field.

What I mean is that I need for this to happen:
User types in Value 1 in Field2008, then Value 2 in Field2007, and Acrobat automatically shows the difference as + or - XX% in FieldChange.

So if Field2008 has "200" and Field2007 has "150" then Field 3 automatically calculates and shows "+33%" (an increase of 50 from 150 is a positive increase of 33%, right?)

I have no idea where to get the script for my FieldChange to make this happen.

Thanks!
Alex Adams

My Product Information:
Acrobat Pro 8.0, Windows
George_Johnson
Expert
Registered: Jul 6 2008
Posts: 1876
Take the difference between the second value and the first, divide by the first, and multiply by 100, and round the result. If the form was created in Acrobat, you could use something like the following as the custom calculation script of the third field:

var v1 = getField("Field2007").value;
var v2 = getField("Field2008").value;

event.value = Math.round( 100 * (v2 - v1) / v1);

If you're using Designer, you should be able to adapt this.

George
gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4308
Or with the percent difference field formatted to the "Percentage" format with "0" decimal places:

Using the "Custom calculation script":

event.value = ""; // clear fieldif(this.getField("Field2007").value != 0) // do not divide by zeroevent.value = (this.getField("Field2008").value  - this.getField("Field2007").value) / this.getField("Field2007").value;

George Kaiser

knowgood
Registered: Aug 20 2008
Posts: 13
You are both so awesome! I am a new user here, and I must say I'm impressed with how kind folks are. Greetings from San Francisco!

Cheers,
Alex Adams