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

Percentage Calculations

oldboy
Registered: Jun 4 2009
Posts: 19
Answered

I have 3 columns:
 
Retail Price £1.83 Gross Margin £0.93 Gross Percentage 50.70%
 
I need a calculation that will allow me to calculate the Gross Percentage (50.70%)
 
Can any one help, please.
 
Regards
 
Oldboy

My Product Information:
Acrobat, Macintosh
gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4307
Accepted Answer
Since it appears you could be computing this value several times in your form, it might work better to use a function to compute the percentage value, that way you only need to write calculation once and then call the function as necessary:

// document level funciton
function Percent(nItem, nBse) {
/*
compute the percentage that nItem's value of the nBase's value
*/;
// variable for percentage
var nPercentage = 0;
// compute percentage if there is a base value;
if(nBase.toString() != "" | nBase != 0) {
nPercentage = nItem / nBase;
}
return nPercentage;
} // end Percent ;
// end document level function;

You can now use a custom calculation script like:

// custom calculation script;
// variables for field names;
var sGrossRevenue = "Retail Price"; // retail price
var sGrossMargin = "Gross Margin"; // gross margin amount
// get the base field value;
var nGrossRevenue = this.getField(sGrossRevenue).value;
// get item fields value;
var nGrossMargin = this.getField(sGrossMargin).value;
// set the default result format;
AFNumber_Format(0, 0, 0, 0, "",false);
if(nGrossRevenue == 0) {
event.value = "";
} else {
// compute percentage;
event.value = Percent(nGrossMargin, nGrossRevenue);
// format result as a percentage;
AFPercent_Format(2, 0);
}

The above script will display an empty string if Gross Revennue is zero or balnk.

George Kaiser

oldboy
Registered: Jun 4 2009
Posts: 19
Hi George

I've tried pasting your calculation script into the custom calculation script box and it says there's a 'syntax' error, can you please give me more help.

Regards

Oldboy
gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4307
Since I copied the script from a working form and posted. I have also copied the posted script and pasted into the form without any errors. I removed and text that was not Acrobat JavaScript or a comment. But I use a very plain text editor.

There are many possible errors when cutting and pasting and the error you text you listed is not the complete error text. It is only the most general error, so it is pretty hard to tell what got corrupted or if you made any changes what errors you may have introduced.

Copying and Pasting Code by Thom Parker.

George Kaiser