I tried using the code above, but the results just don't match Excel (or Google Spreadsheet).
I donβt know if I still need to implement this function, but in any case I looked at how this algorithm was built, and although I could not access the excel source code (or google worksheet), I found that it is not simple payment. You can read about this math here:
https://brownmath.com/bsci/loan.htm#Eq8
A function in PHP might look something like this:
function rate($nprest, $vlrparc, $vp, $guess = 0.25) { $maxit = 100; $precision = 14; $guess = round($guess,$precision); for ($i=0 ; $i<$maxit ; $i++) { $divdnd = $vlrparc - ( $vlrparc * (pow(1 + $guess , -$nprest)) ) - ($vp * $guess); $divisor = $nprest * $vlrparc * pow(1 + $guess , (-$nprest - 1)) - $vp; $newguess = $guess - ( $divdnd / $divisor ); $newguess = round($newguess, $precision); if ($newguess == $guess) { return $newguess; } else { $guess = $newguess; } } return null; }
chuckedw
source share