deoiub Posted February 3, 2015 Share Posted February 3, 2015 I'm attempting to create a PHP function that will calculate a loan's interest rate based on knowing $total_loan_amount, $total_number_of_payments, and $payment_amount. I have found a website with the a JS calculator for this, and includes the function I need to use. https://www.easycalculation.com/mortgage/interest-rate.php However, I'm having a difficult time trying to figure out how to translate this to PHP. I'm getting hung up on calculating q, and specifically the 'divide by log2' portion. Anyone have any suggestions on how to write the q = formula in PHP? Cheers Quote Link to comment Share on other sites More sharing options...
soycharliente Posted May 18, 2016 Share Posted May 18, 2016 (edited) Looking at the JavaScript version of the calculation: function calc() { var de = document.getElementById("l").value ; var nu = document.getElementById("n").value ; var pay = document.getElementById("p").value ; var n = nu * 12; var q1 = Math.log(1 + parseFloat(1/n)); var q2 = Math.log(2); var q = parseFloat(q1/q2); var amt; var amt1 = Math.pow((1 + parseFloat(pay/de)),1/q).toFixed(4); var amt2 = parseFloat( Math.pow(amt1-1,q)).toFixed(4); amt = parseFloat(1200 * (amt2 - 1)).toFixed(2); document.getElementById("op").value = amt; } Try something like this to get started: function calc($total_loan_amount, $total_number_of_payments, $payment_amount) { $p = $payment_amount; $a = $total_loan_amount; $n = $total_number_of_payments; $q = log(1 + (1/$n)) / log(2); $i = pow(pow(1 + ($p/$a), 1/$q) - 1, $q) - 1; return $i; } Absolutely no attention paid to data types, decimals, etc for the numbers. Edited May 18, 2016 by charlieholder Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.