# Recipe 5.18 Calculating the Loan (Mortgage) You Can Afford

#### 5.18.1 Problem

You want to calculate how large a loan you can get assuming you make a given monthly payment.

#### 5.18.2 Solution

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

#### 5.18.3 Discussion

Suppose you are a bank with a big pile of money and you want to lend it to someone to earn some interest. The borrower might come to you and say, "I can pay you back \$1,000 per month for 30 years. How much money will you loan me?" The answer requires you to calculate how much the money that you will receive at various times in the future (as a series of periodic payments) is worth today.

The following custom Math.PV( ) function returns the same value as the one obtained in Microsoft Excel using the Insert Function Financial PV formula. You can add this to your Math.as file for easy inclusion in other projects.

```Math.PV = function (i, n, PMT) {
// i   = periodic interest rate
// n   = number of payment periods
// PMT = periodic payment

// Present value compounded over n periods:
// PV = PMT * ( (multiplier-1)/(i*multiplier))
// where multiplier = Math.pow ((1 + i), n)

multiplier = Math.pow ((1 + i), n);
PV = PMT * (multiplier-1) / (i*multiplier);
return PV;
};```

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

```// How much will the bank loan me if I can pay \$1000/month for 30 years (6% rate)?
trace (Math.PV(.06/12, 30*12, 1000));```

Recipe 5.6, Recipe 5.16, Recipe 5.17, and Recipe 5.19. You can consult a financial textbook for the derivation of the preceding formula. It is essentially based on the reverse of the process used in Recipe 5.16 to calculate the future value. .   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