Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

JavaScript Function for Calculating Annual Percentage Rate (APR Formula) Read more: JavaScript Func

Participant ,
Nov 01, 2022 Nov 01, 2022

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.

TOPICS
JavaScript
2.0K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 01, 2022 Nov 01, 2022

What happens when you use the function?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Nov 01, 2022 Nov 01, 2022

No results appear. In the console the result is "undefined"

photo.JPGexpand image

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 01, 2022 Nov 01, 2022

This is correct. You defines a function but doesn't use it.

APR();

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Nov 01, 2022 Nov 01, 2022

with - function APR (num.CD.AmountFinanced, numPaymentsV, num.Financing.RateFinal) - the console result is
SyntaxError: missing) after formal parameters
1: Console: Exec
undefined

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 01, 2022 Nov 01, 2022

What want you do with this?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Nov 01, 2022 Nov 01, 2022
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 01, 2022 Nov 01, 2022

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".

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Nov 01, 2022 Nov 01, 2022

do you mean so?

image.pngexpand image

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 01, 2022 Nov 01, 2022

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();

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Nov 01, 2022 Nov 01, 2022

if so the output is "undefined"

image.pngexpand image

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 01, 2022 Nov 01, 2022

Use the script at calculation of field "num.CD.APREstimate".

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 01, 2022 Nov 01, 2022

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Nov 02, 2022 Nov 02, 2022

Hi. Yes, I posted the screenshot of the Console window

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 02, 2022 Nov 02, 2022
LATEST

Using event.value in the console makes no sense.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines