tags:

views:

27

answers:

2

Hi all,

I'm converting some rather complex calculations from an Excel spreadsheet, to PHP. I'm stuck on the conversion of Excel's FV function, which is defined thusly:

FV( interest_rate, number_payments, payment, PV, Type )

I've been working on this for 2 hours now, and there has to be something I'm missing. Essentially, I need to replicate this functionality into an equivalent PHP function, taking all of the aforementioned arguments.

Any help would be greatly appreciated.

+1  A: 
function fv($r,$n,$p,$pv=0)
{
   $sum = $pv;
   for ( $i=0;$i<$n;$i++ )
   {
       $sum += $sum*$r + $p;
   }
   return $sum;
}

echo fv(0.1,4,10,100);

The extension of the Type parameter is left as an excercise to the reader.

mvds
Thanks a lot for your help, but I am not getting the answer I'm supposed to be getting from this function.My param values are as follows, in correct order:0.0149562574418,4,43.875,-250- The value I am supposed to be getting back (as I am from the Excel document), is 85.82 - any ideas? :S
Paul
Nevermind - works great! Thank you!
Paul
A: 

Slightly modified from the PHPExcel function library:

/**
 * FV
 *
 * Returns the Future Value of a cash flow with constant payments and interest rate (annuities).
 *
 * @param   float   $rate   Interest rate per period
 * @param   int     $nper   Number of periods
 * @param   float   $pmt    Periodic payment (annuity)
 * @param   float   $pv     Present Value
 * @param   int     $type   Payment type: 0 = at the end of each period, 1 = at the beginning of each period
 * @return  float
 */
function FV($rate = 0, $nper = 0, $pmt = 0, $pv = 0, $type = 0) {

    // Validate parameters
    if ($type != 0 && $type != 1) {
        return False;
    }

    // Calculate
    if ($rate != 0.0) {
        return -$pv * pow(1 + $rate, $nper) - $pmt * (1 + $rate * $type) * (pow(1 + $rate, $nper) - 1) / $rate;
    } else {
        return -$pv - $pmt * $nper;
    }
}   //  function FV()


echo FV(0.0149562574418, 4, 43.875, -250);

returns 85.818510876629

Mark Baker