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

How to calculate a percentage in a form

isaanderson
Registered: Aug 11 2008
Posts: 72
Answered

Hi,
I have a form in acrobat 8 pro where I need to add a simple percentage calculation but I do not how. Here is what I have...
Field A (which can have a value from 1-5 that the user will fill out) needs to be multiplied by Field B (which is a % value between 1-100% that the user will fill out) And then I have field C which is the total of the multiplication of field A x Field B.

I also need field B to be displayed as 10% when the user enters 10 instead of the default 1,000%.

So more simplified... This is what I need to do:
3 x 20% = 0.60 (but the "3" and 20% will change accordingly to what the user enters, of course)

Can somebody PLEASE help me? or give an script to do all this?

Thanks in Advance,

Isa

My Product Information:
Acrobat Pro 8.0, Macintosh
hoangta8
Registered: Nov 28 2007
Posts: 8
Hi Isa,

Hope this would help!

1) "I also need field B to be displayed as 10% when the user enters 10 instead of the default 1,000%. "
// this code is placed in Format tab/Custome Format Script.


if (event.value != "") {
event.value = event.value + "%"
}

2) "3 x 20% = 0.60 (but the "3" and 20% will change accordingly to what the user enters, of course)"


//Place this code in Calculate tab/custome calculation script.


var a = this.getField("Field A");
var b = this.getField("Field B");
event.value = a.value * (b.value / 100)


HT
gkaiseril
Online
Expert
Registered: Feb 23 2006
Posts: 4307
The Percent format displays the decimal amount multiplied by 100 and adds the "%" symbol, but keeps the decimal value for calculations. If you use a custom format script to add the "%" sign you will also need to convert the percentage field back to a decimal value.

George Kaiser

isaanderson
Registered: Aug 11 2008
Posts: 72
HT,
I could not make work this code:

if (event.value != "") {
event.value = event.value + "%"
}

I think it has something to do with what gkaiseril is saying (I need to convert the percentage field back to a decimal value)

Anyway, I fixed that part just by putting the % (character) outside the field so it would display as non-editable text in the pdf. So the user will only type a number without the % character.

The rest of the code for fields A, B and C worked beautifully!

THANKS FOR YOUR HELP HT!
gkaiseril
Online
Expert
Registered: Feb 23 2006
Posts: 4307
The percent sign is a special string character and its presence causes a field to be a character string and not a numeric field in Acrobat JavaScript.

George Kaiser

TrishPRRD
Registered: Feb 17 2010
Posts: 8
I used the above information to calculate % from dollar values (i.e,. 1 + 2 = TOTAL, then 1/TOTAL = %) in my form. Here is the script I used:

to calculate % from dollar amount fields like on the GIA forms use this Java script:
var a =this.getField("1");
var b =this.getField("TOTAL");
event.value=(a.value/b.value)

note: where 1 is the field name of the numerator and TOTAL is the field name of the denominator

When I enter information in my form in other fields I get the following error message "the value entered does not match the format of the field [Percent B]" adn then it cues me to enter a value in the % field further down the document where the % shoudl be automatically calculated for me. If I enter the dollar values in fields 1 and 2 first then the error message does not occur.

Can anyone help? I know that my users of the form will just get discouraged if error messages start popping up when they go to enter information in the fields before the fields I described above.

thanks
Lubu
Registered: Mar 19 2010
Posts: 6
Hi,

I'm new user of acrobat 9.

I'm trying to calcalute a percentage and I'm having problems with calculation script
var c = Number(this.getField("P3").value);
var d = Number(this.getField("P4").value);
var T2=(d/c)*100;
if(c < 1 || isNaN(T2))
event.value = 0;
else
event.value = T2;

When I put format on T2 as "Number" in properties if I insert P3=10 and P4=10 I get the result 100.00 and if I change de format to "percentage it shows 10,000.00%.

Please help
Kind Regards
Lubu
gkaiseril
Online
Expert
Registered: Feb 23 2006
Posts: 4307
If you use the the percentage format, you do not need to multiply by 100. Any decimal number entered into the percentage formatted field will be displayed with the decimal point moved 2 places to the right and add the percentage sign. But the value of this field when used in a calculation will be the decimal number.

You may also need to be careful not to divide by zero.

George Kaiser

Lubu
Registered: Mar 19 2010
Posts: 6
Dear gkaiseril,

I took the 100 and format the field as percentage and it work.

About dividing to zero how can I avoid in my example the field P3 to accept zero and give a message box to the user that we can only input values > 1I'm a novice doing some forms using trial error approach as member of a cultural non-profit organization in a voluntary manner. If is not asking too much, woul you mind to recommend me where can I find/ download some useful scripts

Thanks

Lubu
elsecurie
Registered: Jul 7 2010
Posts: 1
How do i fix this code to avoid receiving the message "entered value does not match field format"
var c=this.getField("4").value
var d=this.getfField("3").value
event.value=c/d

G Sanchez

gkaiseril
Online
Expert
Registered: Feb 23 2006
Posts: 4307
Quote:
You may also need to be careful not to divide by zero.
Do [b]not[/b] divide by zero or [b]null[/b], which is treated like zero in math computations.

You can change the field format where the calculation is being preformed to 'None' and observe the result for various inputted values.

You may also want to check the result of the calculation for the 'Infinity' values.

George Kaiser