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

Calculation for percentage

jaynefergs
Registered: Nov 11 2008
Posts: 37
Answered

How do you script percentage and have it show the answer with percentage?

Here is my problem. I have hours worked for one company and total hours worked. I need to divide the total hours of the first company by the total hours worked.

Example: Total TPP hours/Total Hours worked = giving me the percentage worked for TPP.

15hr/160hrs= 9% (I need it rounded)

My Product Information:
Acrobat Pro 8.0, Windows
George_Johnson
Expert
Registered: Jul 6 2008
Posts: 1876
Do you need the field value to be rounded, or just what gets displayed? For example, 15/160 is equal to 0.09375. If you set up the field to have a Percentage format category and specify 0 decimal places, the field value of 0.09375 will get displayed as 9%. If you then use this field value in other calculations (a sum of several fields, for example), then things may not appear to add up correctly due to these rouding errors.

Also, exactly what do you want to happen if any of the input fields are blank or zero?

A custom calculation script that might do what you want could look something like:

var v1 = getField("Total_TPP_Hours).value;var v2 = getField("Total_TPP_Hours).value; if (v2 != 0) { // Perform calculation and set this field valueevent.value = v1 / v2; } else { // Blank this field value if dividing by zeroevent.value = ""; }

This code assusmes the input fields have a numeric format category and that the calculated field has a percentage format category.

George
jaynefergs
Registered: Nov 11 2008
Posts: 37
I get a message: unterminated string literal 1; at line 2

var v2 = getField("Total_TPP_Hours).value;

My Total TPP hours and Total hours worked are number fields and my percentage field is listed as percent with 0.

My hours counted had to be put as text because I have Saturday/Sundays listed, but the total TPP Hour field and the Total Hours worked field are number fields.

I only need the percentage rounded so it shows just 9%
George_Johnson
Expert
Registered: Jul 6 2008
Posts: 1876
> var v2 = getField("Total_TPP_Hours).value;Sorry about that. It shoudl be:

var v2 = getField("Total_TPP_Hours").value;


George
jaynefergs
Registered: Nov 11 2008
Posts: 37
It is giving me the same error. My field name is without the underscore;

Total TPP Hours/Total Hours Worked= Percentage

Would that make a difference?
George_Johnson
Expert
Registered: Jul 6 2008
Posts: 1876
Yes, replace the field names that I assumed with the actual names of your fields.

George
jaynefergs
Registered: Nov 11 2008
Posts: 37
I replaced field names even added the (") and it still gives me the same message.

Could you write the script again.

Here is my exact field names: Total TPP Hours/Total Hours Worked = Percentage

I really appreciate your patience with this problem. Jayne
George_Johnson
Expert
Registered: Jul 6 2008
Posts: 1876
OK, the following should work. Place this code as the custom Calculation script for the Percentage field:

var v1 = getField("Total TPP Hours").value;var v2 = getField("Total Hours Worked").value; if (v2 != 0) { // Perform calculation and set this field valueevent.value = v1 / v2; } else { // Blank this field value if dividing by zeroevent.value = ""; }

Sorry for the previous typos.

George
jaynefergs
Registered: Nov 11 2008
Posts: 37
Thanks so very much George............It worked!!!! Jayne
cbridges
Registered: Jan 12 2010
Posts: 1
I was searching the forums for this very problem. I put the codes in. It works but the only calculation I can get is 0%. I am very new to the calculation thing, as I always used Excel. What can I look for to see what I am doing wrong. I have the numbers to be divided formatted as numbers with no decimal points and the calculation cell formatted as a percent with no decimals if that helps.