I have been playing around making amortization schedules in PHP. My
php.ini currently has the precision set to
14. I understand going into this that there will be rounding errors however I am hoping to minimize these errors.
My current way of doing this is only rounding my payment amounts and then leaving all other values at their
php default precision value. Calculations below:
$Term_Rate = $Annual_Rate / $Year_Terms;// annual_rate / terms per year $Payment = round(( $Term_Rate * $Principle ) / ( 1 - (( 1 + $Term_Rate )**( -$Total_Terms ))), 2 ); if($y%4===0) $leap = 1; $interval = $date_2->diff($date_1); $days = $interval->days; $interest = $days * (( $Annual_Rate * $Principle ) / ( 365 + $leap )); if($Payment>($Principle+$interest)) $Payment = round( $Principle + $interest, 2); $balance = ( $Principle + $interest ) - $Payment; $Principle=$balance;
Then for any values that are displayed back I do my rounding to 2 decimal places. For most of the inputs I have tried the calculator performs well, meaning the loan is paid of on the final payment of the term. However, some input values can cause my loan to be paid off early. Inputs such as:
$Pay_Type = "Monthly"; $Principle = "25000000.00"; $Annual_Rate = "0.2"; $Term_Length = "360"; $Term_Unit = "Months"; $Start_Date = "2020-01-31";
Not all of the inputs will be realistic but Im hoping to be able to handle even some of the more obsurred inputs such as a 90% interest rate on $25000000.
Please inform me if more of my code is required or if any calculations are handled improperly.