27 Replies Latest reply on Jul 26, 2017 4:24 PM by dennisb777

    How to calculate "Future Value" of an investment?

    dennisb777

      Does anyone now the javascript formula to have a form field in Adobe Acrobat Pro calculate "Future Value" given present value, years & interest?

        • 1. Re: How to calculate "Future Value" of an investment?
          Karl Heinz Kremer Adobe Community Professional

          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)

          • 2. Re: How to calculate "Future Value" of an investment?
            dennisb777 Level 1

            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.

            AssetSnapshot.jpg

            • 3. Re: How to calculate "Future Value" of an investment?
              try67 MVP & Adobe Community Professional

              If you want additional help you will need to provide the exact formula you want to use for this calculation.

              • 4. Re: How to calculate "Future Value" of an investment?
                Karl Heinz Kremer Adobe Community Professional

                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.

                • 5. Re: How to calculate "Future Value" of an investment?
                  dennisb777 Level 1

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

                  }

                  • 6. Re: How to calculate "Future Value" of an investment?
                    dennisb777 Level 1

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

                    • 7. Re: How to calculate "Future Value" of an investment?
                      Bernd Alheit Adobe Community Professional & MVP

                      Where do you use the function FV?

                      • 8. Re: How to calculate "Future Value" of an investment?
                        dennisb777 Level 1

                        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.

                        • 9. Re: How to calculate "Future Value" of an investment?
                          Bernd Alheit Adobe Community Professional & MVP

                          I miss following in your code:

                          event.value = FV(a, b, c, d);

                          • 10. Re: How to calculate "Future Value" of an investment?
                            dennisb777 Level 1

                            Thank you Mr. Alheit, I added your correction, now I am receiving the following syntax error.  Please see pic.

                            syntax.error.jpg

                            • 11. Re: How to calculate "Future Value" of an investment?
                              Bernd Alheit Adobe Community Professional & MVP

                              Why did you remove the function definition?

                              function FV(a, b, c, d) {

                              • 12. Re: How to calculate "Future Value" of an investment?
                                dennisb777 Level 1

                                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?2017-07-25_22-30-49.jpg

                                • 13. Re: How to calculate "Future Value" of an investment?
                                  dennisb777 Level 1

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

                                  • 14. Re: How to calculate "Future Value" of an investment?
                                    Bernd Alheit Adobe Community Professional & MVP

                                    At the end you must add the code of the 9. reply.

                                    • 15. Re: How to calculate "Future Value" of an investment?
                                      dennisb777 Level 1

                                      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!

                                      • 16. Re: How to calculate "Future Value" of an investment?
                                        try67 MVP & Adobe Community Professional

                                        Your function takes five parameters, but you've only defined four in the code above... So there's a mismatch there.

                                        • 17. Re: How to calculate "Future Value" of an investment?
                                          dennisb777 Level 1

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

                                          • 18. Re: How to calculate "Future Value" of an investment?
                                            Bernd Alheit Adobe Community Professional & MVP

                                            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.

                                            1 person found this helpful
                                            • 19. Re: How to calculate "Future Value" of an investment?
                                              try67 MVP & Adobe Community Professional

                                              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.

                                              • 20. Re: How to calculate "Future Value" of an investment?
                                                dennisb777 Level 1

                                                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!

                                                • 21. Re: How to calculate "Future Value" of an investment?
                                                  Test Screen Name Most Valuable Participant

                                                  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?

                                                  • 22. Re: How to calculate "Future Value" of an investment?
                                                    try67 MVP & Adobe Community Professional

                                                    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;

                                                     

                                                    • 23. Re: How to calculate "Future Value" of an investment?
                                                      Test Screen Name Most Valuable Participant

                                                      Hmm, I did a text search to double check but I may have got it wrong. Seems in this case doubly important to find the author's intention.

                                                      • 24. Re: How to calculate "Future Value" of an investment?
                                                        Karl Heinz Kremer Adobe Community Professional

                                                        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

                                                         

                                                         

                                                         

                                                         

                                                        1 person found this helpful
                                                        • 25. Re: How to calculate "Future Value" of an investment?
                                                          gkaiseril MVP & Adobe Community Professional

                                                          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 calculation I would use the following code:

                                                           

                                                          var a = Number(this.getField("Q1").value);  // rate;

                                                          var b = Number(this.getField("Q1Int").value); // nper;

                                                          var c = Number(this.getField("Q1Yr").value); // pmt;

                                                          var d = Number(this.getField("Q1Pmt").value); // pv;

                                                          var e = 1; // type?

                                                          event.value = FV(a, b, c, d, e);

                                                           

                                                          You have not provided any information about the "type" parameter.

                                                           

                                                          From your image I see you have 2 fields with the name "Q2Int" and not one field named "Q1Int". This will generate an error when you try to compute the future value for line one because you have no field named "Q1Int". This error should appear in the Acrobat JavaScript Console if you have it open to set to open upon an error.

                                                          • 26. Re: How to calculate "Future Value" of an investment?
                                                            dennisb777 Level 1

                                                            Mr. Gkaiseril

                                                            THANK YOU for the time you have taken to help me to help others!

                                                            THANK YOU for your kindness!

                                                            THANK YOU SIR for being so considerate!

                                                            You are a programming wizard and your answer worked perfectly--solving a riddle others could not or would not!

                                                             

                                                            May the Lord richly bless you!

                                                            • 27. Re: How to calculate "Future Value" of an investment?
                                                              dennisb777 Level 1

                                                              Thank you as well Karl, your answers set me on the correct path.