You are here

public static function PHPExcel_Calculation_Financial::XNPV in Loft Data Grids 6.2

Same name and namespace in other branches
  1. 7.2 vendor/phpoffice/phpexcel/Classes/PHPExcel/Calculation/Financial.php \PHPExcel_Calculation_Financial::XNPV()

* XNPV * * Returns the net present value for a schedule of cash flows that is not necessarily periodic. * To calculate the net present value for a series of cash flows that is periodic, use the NPV function. * * Excel Function: * =XNPV(rate,values,dates) * *

Parameters

float $rate The discount rate to apply to the cash flows.: * @param array of float $values A series of cash flows that corresponds to a schedule of payments in dates. The first payment is optional and corresponds to a cost or payment that occurs at the beginning of the investment. If the first value is a cost or payment, it must be a negative value. All succeeding payments are discounted based on a 365-day year. The series of values must contain at least one positive value and one negative value. * @param array of mixed $dates A schedule of payment dates that corresponds to the cash flow payments. The first payment date indicates the beginning of the schedule of payments. All other dates must be later than this date, but they may occur in any order. * @return float

1 call to PHPExcel_Calculation_Financial::XNPV()
PHPExcel_Calculation_Financial::XIRR in vendor/phpoffice/phpexcel/Classes/PHPExcel/Calculation/Financial.php

File

vendor/phpoffice/phpexcel/Classes/PHPExcel/Calculation/Financial.php, line 2162

Class

PHPExcel_Calculation_Financial
PHPExcel_Calculation_Financial

Code

public static function XNPV($rate, $values, $dates) {
  $rate = PHPExcel_Calculation_Functions::flattenSingleValue($rate);
  if (!is_numeric($rate)) {
    return PHPExcel_Calculation_Functions::VALUE();
  }
  if (!is_array($values) || !is_array($dates)) {
    return PHPExcel_Calculation_Functions::VALUE();
  }
  $values = PHPExcel_Calculation_Functions::flattenArray($values);
  $dates = PHPExcel_Calculation_Functions::flattenArray($dates);
  $valCount = count($values);
  if ($valCount != count($dates)) {
    return PHPExcel_Calculation_Functions::NaN();
  }
  if (min($values) > 0 || max($values) < 0) {
    return PHPExcel_Calculation_Functions::VALUE();
  }
  $xnpv = 0.0;
  for ($i = 0; $i < $valCount; ++$i) {
    if (!is_numeric($values[$i])) {
      return PHPExcel_Calculation_Functions::VALUE();
    }
    $xnpv += $values[$i] / pow(1 + $rate, PHPExcel_Calculation_DateTime::DATEDIF($dates[0], $dates[$i], 'd') / 365);
  }
  return is_finite($xnpv) ? $xnpv : PHPExcel_Calculation_Functions::VALUE();
}