Excel PMT function in Actionscript

Just thought id share this as it will come in useful for someone in the future. Ever wanted to use the PMT function from Excel in Flash? Well, after some digging, and after some modifications, here is it.

PMT = function(r,np,pv,fv,t) {
/*
r = the percentage rate of the loan. (decimal: 8% = 0.08)
np = number of monthly payments (integer)
pv = present value or principal of the loan (decimal)
fv = future value of the loan (after payments) (decimal)
t = paid before or after (0/1)
*/
  var vPow = Math.pow( ( 1 + r ), np );
  var vT = (t == 0) ? r : ( r /( 1 + r ) );
  return ( (vPow * pv) - fv ) / (vPow - 1) * vT;
}

Don’t say I never give you anything! (Thanks OJ for your hint on consolidating the last 2 lines into 1)

  • http://rant.blackapache.net/ OJ

    I reckon that we could make that a lot more obfuscated than that! How about this:
    CYRIL = function(jane,doris,bert,betty,mavis) {
    /*
    jane = the percentage rate of the loan. (decimal: 8% = 0.08)
    doris = number of monthly payments (integer)
    bert = present value or principal of the loan (decimal)
    betty = future value of the loan (after payments) (decimal)
    mavis = paid before or after (0/1)
    */
      var bernie = 1;
      var alf = Math.pow((bernie+jane ),doris);
      return ((alf*bert)-betty)/(alf-bernie)*((mavis==0)?jane:(jane/(bernie+jane)));
    }

  • dan

    Well, yes, we could have couldn’t we?!

    Why not in the comments for the function give all your characters a bio :)

    Ive seen worse before – as have you :P

  • Guu

    Thank you a lot!!!

  • Mike

    Hey dan!!

    Long time no see – HAPPY BIRTHDAY!!

    PMT – Pre Menstrual Tension?!?!

    Now that is a useful calculator – Now I know when to hide – thanks!!!

    Cheers,
    Mike

  • dan

    Hey Mike :)

    Cheers !! We’re you looking for a way to calculate PMT (either kind) and stumbled across my blog before realising it was both Darrall and my birthdays today? (And don’t forget Frodo or Bilbo :) )