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

Acrobat Math Calculations

LynkFinancial
Registered: May 29 2007
Posts: 4

I have converted a excel spreadsheet to an Adobe Acrobat 8 form. I am trying to recreate the calculations within the form using Javascript. Unfortunately, I do not know the Javascript language. I have completed most of the spreadsheet by trial and error. Now I have two scripts I need help with.... The first is the mortgage payment script. This is what I have thus far...and it's not working
 
var a=this.getField("ir");
var b=this.getField("np");
var c=this.getField("PV");
event.value=(PMT=(a.value*(c.value)*Math.pow((a.value+1),b.value)))/((a.value+1)*(Math.pow((a.value+1),b.value)-1));
 
The other problem is on a debt to income ratio calculation. The calculation works but I need to do some kind of size and/or stacking code...since it errors when I delete one of the variable from the equation to reset the form.
 
var a=this.getField("Total Mo Income1");
var b=this.getField("Total Mo Expenses");
var c=this.getField("Debt to Income Ratio");
event.value=c.value= (b.value/a.value);
 
Any HELP would be greatly appreciated as I am losing sleep over search for the proper scripting! Thanks Much!!!!
[link=mailto:bmiller [at] lynkfinancial [dot] com]bmiller [at] lynkfinancial [dot] com[/link]

My Product Information:
Acrobat Pro 8, Windows
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
For not knowing JavaScript you've done a great job so far:)

There aren't any scripting issues with the first calculation. What's the problem?

The second calculation has a divide by zero issue. One way to deal with this is to provide a default value to guard against resets. Something else to consider is testing "a.value" before doing the calculation.

if(a.value == 0 || isNaN(a.value))
event.value = "NA";
else
... do calculation ...

You could also force the "Debt to Income Ratio" field to always be a valid value by using the "Validate" event on this field.

Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script

matthewatkins
Registered: Jun 29 2011
Posts: 11
I am having trouble with a similar calculation as LynkFinancial's first one. I am converting an excel sheet to a PDF document. In Excel I have a calculation:
=-PMT(B44,B45,B40)

where:
B44 = 0.7%
B45 = 72
B40 = $82,720

I'm using the calculation posted above:
var a=this.getField("B44");
var b=this.getField("B45");
var c=this.getField("B40");
event.value=(PMT=(a.value*(c.value)*Math.pow((a.value+1),b.value)))/((a.value+1)*(Math.pow((a.value+1),b.value)-1));

In Excel I get a total of $1450.35 but in Acrobat I get $1441

Can anyone help me with what is going wrong??
try67
Expert
Registered: Oct 30 2008
Posts: 2398
In Acrobat the percentage field value is actually a number between 0 and 1, so your 0.7% is actually 0.007. Have you taken that into account in your calculation?
Also, you should drop the "PMT=" part. It doesn't serve any purpose.

Edit: forgot one zero...

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com

matthewatkins
Registered: Jun 29 2011
Posts: 11
Yes. Thanks. I have taken this into consideration though.
gkaiseril
Online
Expert
Registered: Feb 23 2006
Posts: 4307
You might want to simplify the code. Make sure to check the parenatisies you are using to control the order of calculation.

var a = this.getField("B44").value;
var b = this.getField("B45").value;
var c = this.getField("B40").vlaue;

event.value=( (a * c) * Math.pow( (a + 1), b) )/( (a + 1) * Math.pow( (a + 1), b) - 1 );




George Kaiser

matthewatkins
Registered: Jun 29 2011
Posts: 11
Ok. I replaced with the cleaner code you mentioned gkaiseril, but now my outcome is $1425 where it was $1441. So, still wrong.
try67
Expert
Registered: Oct 30 2008
Posts: 2398
Are the other fields the result of a calculation, or are they filled in by the user?

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com

matthewatkins
Registered: Jun 29 2011
Posts: 11
In this case they are the result of calculations.
try67
Expert
Registered: Oct 30 2008
Posts: 2398
Then check the field calculation order (select the Object Select Tool, then go to Forms - Edit Fields - Set Fields Calculation Order...)
Make sure that this field is below the other fields in the list.

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com

matthewatkins
Registered: Jun 29 2011
Posts: 11
Double checked the calculation order as per your recommendation and it is all in the correct order.
gkaiseril
Online
Expert
Registered: Feb 23 2006
Posts: 4307
Have you checked that the input values are correct?

I see that a parenthesis was in the wrong location.

In the JavaScript Debugging console using the following:

var a = 0.007;
var b = 72;
var c = 82720;

( (a * c * Math.pow( (a + 1), b) ) / ( (a + 1) * Math.pow( (a + 1), b) - 1 );


Result:
1441.0110364167194


George Kaiser

try67
Expert
Registered: Oct 30 2008
Posts: 2398
Maybe Excel is using a somewhat different formula...

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com

matthewatkins
Registered: Jun 29 2011
Posts: 11
I guess it is possible that excel is using a different formula.

George, that still doesn't work. Using the code you provided changing the Parenthesis I got an error saying that I was missing one.
matthewatkins
Registered: Jun 29 2011
Posts: 11
Does anyone else have any suggestions? I've been working on this one calculation for a few day and am pretty frustrated with it at this point.