Skip to main content
Participating Frequently
Oct 18, 2023 3:17 PM UTC
Question

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

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 is closed to new replies. Start a new post to keep the conversation going.

10 replies

Participating Frequently
Oct 18, 2023 9:02 PM UTC

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.

Translate
Thom Parker
Community Expert
Community Expert
Oct 18, 2023 9:26 PM UTC

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

 

Translate
Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
Participating Frequently
Oct 19, 2023 12:01 AM UTC

code is a custom calculation placed inside the calculateBTN

the calculation is run via mouse down action.

 

Translate
Thom Parker
Community Expert
Community Expert
Oct 18, 2023 5:58 PM UTC

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

 

 

Translate
Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
Participating Frequently
Oct 18, 2023 6:07 PM UTC

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.

Translate