Copy link to clipboard
Copied
Hi
I am looking for a javascript function to calculate the Annual Percentage Rate (APR).
On this page https://answers.acrobatusers.com/Formula-loan-interest-rate-q176800.aspx
I found a post from a user explaining the function needed to calculate the APR rate.
I copied this function to the console of my pdf file
function APR ()
{var paymentV = + getField ("num.CD.Monthly.P & I"). value; // MONTHLY P&I PAYMENT
var numPaymentsV = + getField ("num.numPayments"). value; // NUMBER OF PAYMENTS, DURATION OF THE LOAN
var rateLoanV = + getField ("num.Financing.RateFinal"). value; // INTEREST RATE LOAN
var financedV = + getField ("num.CD.AmountFinanced"). value; // AMOUNT OF THE LOAN LESS ADVANCE COSTS
var rateV = rateLoanV / 12; // convert annual rate to rate
var testrate = rateV;
var iteration = 1;
var testresult = 0; // iterate until result = 0
var testdiff = testrate;
while (iteration <= 100)
{testresult = ((testrate * Math.pow (1 + testrate, numPaymentsV)) / (Math.pow (1 + testrate, numPaymentsV) - 1)) - (paymentV / financedV);
if (Math.abs (testresult) <0.0000001) break;
if (testresult <0) testrate + = testdiff;
else testrate - = testdiff;
testdiff = testdiff / 2;
iteration ++;}
testrate = testrate * 12;
var aprEst = this.getField ("num.CD.APREstimate");
aprEst.value = testrate.toFixed (6);}
In the pdf file I have inserted these fields
num.CD.Monthly.P & I = 489
num.numPayments = 300
num.Financing.RateFinal = 0.03
num.CD.AmountFinanced = 100,000
The result from the console is undefined
Please, can anyone help me to set the function correctly?
Or, does anyone know the correct function to calculate the APR rate.
Copy link to clipboard
Copied
What happens when you use the function?
Copy link to clipboard
Copied
No results appear. In the console the result is "undefined"
Copy link to clipboard
Copied
This is correct. You defines a function but doesn't use it.
APR();
Copy link to clipboard
Copied
with - function APR (num.CD.AmountFinanced, numPaymentsV, num.Financing.RateFinal) - the console result is
SyntaxError: missing) after formal parameters
1: Console: Exec
undefined
Copy link to clipboard
Copied
What want you do with this?
Copy link to clipboard
Copied
I wanted to recall the variables as it was done in this site https://www.webmasterymadesimple.com/blog/javascript-function-for-calculating-annual-percentage-rate...
Copy link to clipboard
Copied
Change the last two lines of the function from this:
var aprEst = this.getField("num.CD.APREstimate");
aprEst.value = testrate.toFixed(6);
To this:
event.value = testrate.toFixed(6);
And then call it from the custom Calculation script of "num.CD.APREstimate".
Copy link to clipboard
Copied
do you mean so?
Copy link to clipboard
Copied
Yes, but without the "undefined" part. That's just the output of running it in the console. It should not be a part of the code.
Under the Calculate event just enter:
APR();
Copy link to clipboard
Copied
if so the output is "undefined"
Copy link to clipboard
Copied
Use the script at calculation of field "num.CD.APREstimate".
Copy link to clipboard
Copied
We explained how it should be used. It's not clear from these screenshots where you're placing this code. Is this the Console window? If so, that's not how it should be used. If you want help you need to provide the complete details of what you're doing, and what the results are.
Copy link to clipboard
Copied
Hi. Yes, I posted the screenshot of the Console window
Copy link to clipboard
Copied
Using event.value in the console makes no sense.