# Recipe 5.17 Calculating Retirement Savings

#### 5.17.1 Problem

You want to calculate how much money you'll have at retirement, assuming you'll receive a series of equal periodic payments.

#### 5.17.2 Solution

Calculate the future value of a series of periodic payments using the custom Math.FV( ) function.

#### 5.17.3 Discussion

Suppose you are implementing a retirement calculator. People commonly deposit a certain amount of money each month into their retirement account. Typically, they want to know how much money they'll accumulate in the account at some time in the future. There are two components to the accumulation of funds: the interest earned each period, plus the periodic payment that is added. The total value also includes a third component: the principal in the account at the beginning of the calculation.

Here is an enhanced version of the futureValue( ) function presented in Recipe 5.16. It calculates the future value based on an initial payment plus periodic payments. We calculate the final amount by adding together the appreciation of the principal and the appreciation of the periodic payments. If the periodic payment is 0, the calculation reduces to the equation from Recipe 5.16 (in which there is principal and interest only). If there is no initial deposit, you can use 0 for the PV parameter. You can add this to your Math.as file to use it in other projects.

```Math.FV = function (interest, n, PMT, PV) {
// PV       = initial deposit (present value)
// interest = periodic interest rate
// n        = number of payment periods
// PMT      = periodic payment
// FV       = future value

// Financial equations for a series of payments compounded over n periods:
// multiplier      = Math.pow ((1 + interest ), n)
// FV of principle = PV * Math.pow ((1 + interest ), n)
//                 = PV * multiplier
// FV of payments  = PMT * ((Math.pow ((1 + interest ), n) - 1) / interest)
//                 = PMT * ((multiplier-1)/interest)

multiplier = Math.pow ((1 + interest), n);
// Add FV of principal and FV of payments together to get total value.
FV = PV * multiplier + PMT * ( (multiplier-1) / interest);

return FV;
};```

You can use the custom Math.FV( ) method as follows:

```// How much money will I have in 30 years if it appreciates at 6%, starting with
// \$50,000 and paying \$400/month?
trace (Math.FV(.06/12, 30*12, 400, 50000));```

Again, you can consult a financial textbook for the derivation of the preceding formulas. You can confirm their accuracy by testing them against the value derived from the brute-force method:

```Math.bruteFV = function (interest, n, PMT, PV) {
FV = PV;
for (var periods = interest; periods <= n; periods++) {
FV = FV * (1 + interest) + PMT;
}
return FV;
};``` You can and should confirm your calculations against the values returned by a commercial spreadsheet or actuarial table. For example, our custom Math.FV( ) function returns the same value, as the one obtained in Microsoft Excel using the Insert Function Financial FV formula.

Recipe 5.6, Recipe 5.16, Recipe 5.18, and Recipe 5.19  Preface  Part I: Local Recipes  Chapter 1. ActionScript Basics  Chapter 2. Runtime Environment  Chapter 3. Color  Chapter 4. Drawing and Masking  Chapter 5. Numbers and Math  Introduction  Recipe 5.1 Representing Numbers in Different Bases  Recipe 5.2 Converting Between Different Number Systems  Recipe 5.3 Rounding Numbers  Recipe 5.4 Inserting Leading or Trailing Zeros  Recipe 5.5 Formatting Numbers for Display  Recipe 5.6 Formatting Currency Amounts  Recipe 5.7 Generating a Random Number  Recipe 5.8 Simulating a Coin Flip  Recipe 5.9 Simulating Dice  Recipe 5.10 Simulating Playing Cards  Recipe 5.11 Generating a Unique Number  Recipe 5.12 Converting Angle Measurements  Recipe 5.13 Calculating the Distance Between Two Points  Recipe 5.14 Determining Points Along a Circle  Recipe 5.15 Converting Between Units of Measurement  Recipe 5.16 Calculating Asset Appreciation (Future Value)  Recipe 5.17 Calculating Retirement Savings  Recipe 5.18 Calculating the Loan (Mortgage) You Can Afford  Recipe 5.19 Calculating Loan Amortization or Annuities  Chapter 6. Arrays  Chapter 7. Movie Clips  Chapter 8. Text  Chapter 9. Strings  Chapter 10. Dates and Times  Chapter 11. Forms  Chapter 12. Objects and Custom Components  Chapter 13. Programming Sound  Part II: Remote Recipes  Part III: Applications  Appendix A. Unicode Escape Sequences for Latin 1 Characters  Colophon