Copy link to clipboard
Copied
Does anyone now the javascript formula to have a form field in Adobe Acrobat Pro calculate "Future Value" given present value, years & interest?
You are not passing the values for the parameters of the function in the function call. It looks like you are also trying to define the function when you are setting the events. value.
I would create a document level script named "FV" and place the following code into script.
function FV(rate, nper, pmt, pv, type) {
var pow = Math.pow(1 + rate, nper), fv;
if (rate) {
fv = (pmt*(1+rate*type)*(1-pow)/rate)-pv*pow;
} else {
fv = -1 * (pv + pmt * nper);
}
return fv.toFixed(2);
}
For the field
...Copy link to clipboard
Copied
This actually has nothing to do with Acrobat's implementation of JavaScript: The advantage of using JavaScript as a programming language inside Acrobat is that you can use any JavaScript function that does only use core JavaScript language features in your script in Acrobat. This means that you can google for e.g. "future value investment javascript" and find solutions that will work for you (e.g. here: excel - How to calculate Future Value (FV) using JavaScript - Stack Overflow)
Copy link to clipboard
Copied
Thank you Karl for your valiant effort to help me, but unfortunately, you are dealing with someone illiterate in Java. I was actually astute enough to do the same search and found the same formula, but after several attempts, I can’t seem to modify the variables to work in my form. Plus, I don’t know what the “type” variable has to do with anything. In addition, I don’t need the “payment” variable, but can input a field for it. I have included an attachment so you can see what I am working with if you have the desire to help once again.
Thank you in advance for your consideration and time.
Copy link to clipboard
Copied
If you want additional help you will need to provide the exact formula you want to use for this calculation.
Copy link to clipboard
Copied
Here is another recent attempt, but it did not work either:
var a = Number(this.getField("Q1").value);
var b = Number(this.getField("Q1Int").value);
var c = Number(this.getField("Q1Yr").value);
var d = Number(this.getField("Q1Pmt").value);
function FV(a, b, c, d) {
var pow = Math.pow(1 + b, c),
fv;
if (b) {
fv = (d*(1+b)*(1-pow)/b)-a*pow;
} else {
fv = -1 * (a + d * c);
}
return fv.toFixed(2);
Copy link to clipboard
Copied
Where do you use the function FV?
Copy link to clipboard
Copied
Thank you in advance for any help you can offer. I posted a picture of the adobe form spreadsheet above. You can see the column titled Future Value. I want the spreadsheet to calculate the future value of each qualified asset I place in the form (one per row). I feel like the formula I posted above is close, but I am obviously missing something. Thanks again for your insight.
Copy link to clipboard
Copied
I miss following in your code:
event.value = FV(a, b, c, d);
Copy link to clipboard
Copied
Thank you Mr. Alheit, I added your correction, now I am receiving the following syntax error. Please see pic.
Copy link to clipboard
Copied
Why did you remove the function definition?
function FV(a, b, c, d) {
Copy link to clipboard
Copied
Because I have been trying many different variations to get the formula to work. That just happened to be the last attempt before I emailed you. I inserted back in in the pic below. Now the syntax error is gone, but the formula still does not generate a number; plus no one can seem to tell me what "type" means in this equation (which I arbitrarily replaced with another variable just to have a number for it to process--but it still doesn't work. Can you help me?
Copy link to clipboard
Copied
At the end you must add the code of the 9. reply.
Copy link to clipboard
Copied
First, I want to once again thank you Mr. Alheit for continuing to try and help me, however I do not understand what the "code of the 9" is; much less how to add it at the end. When I consider the other working formulas in this adobe form, most of them end with some variation of this: "event.value =((a+b+c)-d);"
Is this what you are referring to or is it something different. Once again, here is the formula I was given.
function FV(rate, nper, pmt, pv, type) {
var pow = Math.pow(1 + rate, nper),
fv;
if (rate) {
fv = (pmt*(1+rate*type)*(1-pow)/rate)-pv*pow;
} else {
fv = -1 * (pv + pmt * nper);
}
return fv.toFixed(2);
}
Can you please type in what to add at the end -- and show me the proper syntax? Thank you so much!
Copy link to clipboard
Copied
Your function takes five parameters, but you've only defined four in the code above... So there's a mismatch there.
Copy link to clipboard
Copied
Yes sir, I know. If you read through the conversations above, you will see that I have repeatedly asked everyone what "type" means in the equation, but no one seems to know the answer. I have been calculating future values for years and have never had to include "type" into the formula; so apparently, it is something specific to javascript, but no one can tell me. What is "type" and is it critical to calculating FV? I don't know the answer. I wonder if it can be removed from the equation? Therefore, I am very open to anyone's suggestions that can help me make this formula work. Please advise...
Copy link to clipboard
Copied
You want us to do your research for you? It's your function. You should find out how it works and what it means... If you want to remove parts from it, you do so at your own risk.
Copy link to clipboard
Copied
Use this:
var a = Number(this.getField("Q1").value);
var b = Number(this.getField("Q1Int").value);
var c = Number(this.getField("Q1Yr").value);
var d = Number(this.getField("Q1Pmt").value);
function FV(a, b, c, d) {
var pow = Math.pow(1 + b, c),
fv;
if (b) {
fv = (d*(1+b)*(1-pow)/b)-a*pow;
} else {
fv = -1 * (a + d * c);
}
return fv.toFixed(2);
}
event.value = FV(a, b, c, d);
You should learn the basics of JavaScript.
Copy link to clipboard
Copied
Thank you again Mr. Alheit, you have been patient and generous with me. Apparently, everyone is becoming frustrated with me and I am sorry. I have been, and I am still am reading and trying to learn the basics, that is how I have created this 3 page spreadsheet with dozens of its working formulas up to this point. I was just asking for help on this one more complex formula (FV).
By the way, THANK YOU again for your attempt to send me a working formula, that is more than anyone else has done, but it does not work either...no errors, just no results calculated. I will try not to bother you or anyone else again...I don't want any hard feelings. I am simply a financial advisor trying to create a spreadsheet to help my clients. Have a blessed day!
Copy link to clipboard
Copied
I can tell you in this snippet:
function FV(rate, nper, pmt, pv, type) {
var pow = ...
... lines as in earlier post ...
return fv.toFixed(2);
}
that "type" does nothing. It has no special meaning, and does not appear. So it can be anything. However, it must be there with some value or other when you call it. Why is it there? You'd have to ask the author. Perhaps they meant it to do something, or planned to do so in the future, were distracted, or it's a house style, or for debugging.
You may not welcome this insight, but it may be useful: I never, ever, post snippets of code, no matter how useful. Because it leads people into exactly such traps as this, if they are trying to program without fully understanding. I call this "copy and paste programming" and as a programming professional I will sometimes use it, but I have to do so with the utmost care. I know exactly why you'd be looking for a shortcut to save the months of study, but it is a dangerous path: just as you would hardly approve of someone trying to go out as a financial adviser who is untrained, but listens then does Google searches. This is important if you are creating or using a formula which will form the basis of financial advice: who will be responsible if it is wrong?
Copy link to clipboard
Copied
I totally agree about the dangers of programming by copying-and-pasting. However, "type" is used in the function (at least in one of the versions above), in this line:
fv = (pmt*(1+rate*type)*(1-pow)/rate)-pv*pow;
Copy link to clipboard
Copied
Let me repeat what I said very early in this discussion:
You cannot copy and paste your way to a successful solution if you don't know what you are doing.
There are two ways you can get to a working, reliable and maintainable solution:
1) You do the research and learn enough about the problem and the programming environment to be able to take a code snippet you find online and massage it into something that works in your environment.
2) You pay somebody to do the task for you and potentially provide the training along the way so that next time, you can make adjustments to the final product.
You need to go back to where you found this code snippet (the Stack Overflow link from the beginning of this discussion). There you will see that this JavaScript function is a functional equivalent of the Excel function with the same name. You can find out what exactly it does here: FV function - Office Support
You will see that the "type" argument is optional in Excel. Unfortunately, the JavaScript function insists on this parameter being there, so you can always pass in "0" or "1" (meaning the payment is due at beginning or end of period - see the documentation). If you only ever need one of these cases, then the function can be simplified so that "type" is no longer used, but that again requires you to know what you are doing.
The Excel function "FV" may not be exactly what you need in your application - we don't know that because we don't know how exactly your form will work, so that again is something where you need to be able to adjust the functionality in this given function to match your specific needs.
I can tell you that the function provides the same results that are shown in the documentation page that I linked to. Here is even a simple PDF form that shows that: http://khkonsulting.com/files/AUC/FV.pdf
Copy link to clipboard
Copied
Thank you as well Karl, your answers set me on the correct path.
Copy link to clipboard
Copied
Here is the original formula I was given and told would work, can you please tell me what "type" is; and help me modify this formula into a working javascript for my working form?
function FV(rate, nper, pmt, pv, type) { var pow = Math.pow(1 + rate, nper), fv; if (rate) { fv = (pmt*(1+rate*type)*(1-pow)/rate)-pv*pow; } else { fv = -1 * (pv + pmt * nper); } return fv.toFixed(2); }
Copy link to clipboard
Copied
Let me start with this: Do yourself a favor and learn how JavaScript works. You cannot copy and paste your way to a successful solution if you don't know what you are doing. You need to be able to adapt what you find here (and in other places) to your specific requirements, and fix potential problems when the requirements change in the future. Here is some information about how to learn to program in JavaScript for Acrobat: http://khkonsulting.com/2017/01/learning-to-program-javascript-for-adobe-acrobat/
Also, you are not dealign with "Java", this is "JavaScript" - similar name, but completely different environment, and very important when you try to search for solutions. You will not be able to adapt a Java solution to work within Acrobat at all. You can however adapt a JavaScript solution (as long as it's only using the core language) - usually without too many problems.
Here are a few pointers to how you can make this work:
To get the value of a field, you would use this syntax:
var someValue = this.getField("SomeFieldName").value;
To set the current field value in a custom calculation script, you would use this:
event.value = someValue;
This, plus the scripts you've found, plus a good working knowledge of how JavaScript works should be all you need.
Copy link to clipboard
Copied
I have a very basic understanding, and have written the formulas for a hundred other basic calcs in the form; but this FV is kicking my rear...therefore, I will spend some more time in the link you gave to me...thank you. Here is my latest attempt to adapt the script to my form fields, but it doesn't work. I am sure part of the problem is that I don't have variables for pmt or type. I don't even know what "type" is to the equation. I don't need "pmt" so I tried removing both of them, but that didn't work either. I am sorry to be so much trouble. Thank you for any help you can provide.
var a = Number(this.getField("Q1").value);
var b = Number(this.getField("Q1Int").value);
var c = Number(this.getField("Q1Yr").value);
var d = Number(this.getField("Q1Pmt").value);
event.value = function FV(rate, nper, pmt, pv, type) {
var pow = Math.pow(1 + rate, nper),
fv;
if (rate) {
fv = (pmt*(1+rate*type)*(1-pow)/rate)-pv*pow;
} else {
fv = -1 * (pv + pmt * nper);
}
return fv.toFixed(2);
}