i want to calculate the NPV ( Net Present 开发者_C百科Value ) using PHP.
Below is the detail which i have.
discount rate : 15% Cash Flow Values per Year: Year 1: 110000 Year 2: 122000 Year 3: 135200 Year 4: 149720 Year 5: 157706 Year 6: 166091
NPV Formula which i am using.
NPV = sum of all years( (Ci)/(1+r)^i)
i=1,2,3,4,5,...
Ci= Cash Flow for year i
r=rate of discount
Any help.
Thanks In advance
Avinash
You would calculate the NPV of each year using the following formula: (value) / ((1 + 0.15) ^ (year)) then sum all the results to get your final NPV - where ^ means to the power of.
So for year two it would be: 122000 / ((1.15)^ 2).
At the current point in time the power ^ value would be 0
This is taken from the work done by TommySzalapski here: https://www.experts-exchange.com/questions/26875482/Finding-NPV-net-present-value-with-PHP.html
The best way to do it is to first create an array of your cash flows. Remember that arrays start at 0, so if there is no cash flow at year 0 then [0] = 0. If there is a cash out lay make this negative.
$cf = array(0, 110000, 122000, 135200, 149720, 157706, 166091 );
Then create variables for your inputs:
$years = 6;
$discountRate = 0.15;
Then you can use TommySzalapski formula
function npv($rate, $values, $year) {
for ($i=$year;$i>=0;$i-=1) {
$npv = ($values[$i] + $npv) / (1 + $rate);
}
return '$'.number_format($npv,2,'.',' ');
}
so plugin:
$NPV = npv($discountRate, $cf, 6); // You can also use the $years variable here
echo $NPV;
Answer is $445 751.54 which is the same as the NPV formula gives in Excel.
精彩评论