Skip to main content
Participating Frequently
October 18, 2023
Question

PV function works in code pen as expected but not in PDF

  • October 18, 2023
  • 2 replies
  • 1555 views

Howdy folks, I hope this message finds you well this morning/afternoon/evening.


I'm trying to  implement the JS logic I have here in this codepen that is calculating the present value of a loan based on a corresponding payment, and then dividing that total by a the inverse of a downpayment % which is ( 1 - downPaymentRate ).

In the codepen I'm getting expected dollar amounts +/- a few cents in comparison to the  PV formula as it exists in an excel spreadsheet. For whatever reason when I implement this same function in the adobe PDF it's giving me a value thats off by over $8000. I'm not sure what the difference is in the formula perhaps someone else has seen this problem before.


the codepen: https://codepen.io/buff4eyes/pen/XWoLQVN

the code in my PDF:

function calculatePV(rate, nper, pmt, fv) {
  // Calculate the present value using the formula
  var pv = fv / Math.pow(1 + rate, nper);

  // If there are periodic payments (pmt), subtract them from the present value
  if (pmt) {
    pv -= pmt * (1 - Math.pow(1 + rate, -nper)) / rate;
  }

  return pv;
}

var adjustedListPrice = getField("adjustedListPrice");
var downPaymentRate = Number(getField("DOWNPAYMENTRow2").value)
var LLMonthlyPayment = Number(getField("LLPMT2").value);
var rate = Number(getField("MARKETRATERow2").value);
var PVRate = (rate / 12);
var nper = 360;
var calculatedPV = calculatePV(PVRate, nper,-LLMonthlyPayment, 0);
var denominator = (1 - downPaymentRate).toFixed(3);
adjustedListPrice.value = (calculatedPV / denominator).toFixed(2);

when I use the same values as shown in the js file of the codepen I get a substantially different answer. Is the Math.pow function operating differently in the pdf? I'm so confused.

This topic has been closed for replies.

2 replies

Participating Frequently
October 18, 2023

well, i've logged every value that is being input and it matches the values going into the excel formula but for whatever reason adobe is outputting a different value.

Thom Parker
Community Expert
Community Expert
October 18, 2023

So where exactly is the code placed?  Is it a caluculation script?  On which field? and what is the input field?

 

It looks like the intended target is the "adjustedListPrice" field.  If this is a calculation script on that field it probably won't work. You can read about how to write calculation scripts here:

https://www.pdfscripting.com/public/How-to-Write-a-Basic-PDF-Calculation-Script.cfm 

https://www.pdfscripting.com/public/Calculating-field-values-and-more.cfm

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
Participating Frequently
October 19, 2023

code is a custom calculation placed inside the calculateBTN

the calculation is run via mouse down action.

 

Thom Parker
Community Expert
Community Expert
October 18, 2023

I would suggest performing some debug. Run the code manually, line by line in the console window and see where the calculation is going off the rails.   

Here's a video on using the console window:

https://www.pdfscripting.com/public/images/video/AcroJSIntro/AcroJSIntro_ConsoleWindow.cfm

 

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
Participating Frequently
October 18, 2023

Thanks, I've been logging each variable independently to match them with the ones in my excel spreadsheet. Haven't found it yet, but going to continue debugging these logs.