Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Rounded #s for Debt Payments #6

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Fixes & Tests for DebtAmortizator
addressed issues uruba stated in last push
  • Loading branch information
alloylab committed Jul 21, 2016
commit 2e4aad70656e4e26d97374590e1b438e37e2a6cc
46 changes: 23 additions & 23 deletions src/Calculators/DebtAmortizator.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ class DebtAmortizator extends CalculatorAbstract
"days" => "debtLengthInDays"
],
"debtSingleRepayment",
"debtSingleRepayment_Rounded",
"debtSingleRepaymentRounded",
"debtRepayments" => "debtRepaymentsAsArrays",
"debtRepayments_Rounded" => "debtRepaymentsAsArrays_Rounded"
"debtRepaymentsRounded" => "debtRepaymentsAsArraysRounded"
];

/**
Expand Down Expand Up @@ -294,10 +294,10 @@ public function getDebtSingleRepayment()
/**
* @return string [Value of a single debt repayment instance as a string]
*/
public function getDebtSingleRepayment_Rounded() {
public function getDebtSingleRepaymentRounded() {
// single repayment 'K = PV/((1-v^n)/i)'
return MathFuncs::round_up(
MathFuncs::div(
return MathFuncs::roundUp(
MathFuncs::div(
$this->debtPrincipal,
MathFuncs::div(
MathFuncs::sub(
Expand Down Expand Up @@ -341,30 +341,30 @@ public function getDebtRepaymentsAsArrays()
/**
* @return array
*/
public function getDebtRepaymentsAsArrays_Rounded()
public function getDebtRepaymentsAsArraysRounded()
{
$repayments = array();
$i = 1;
$balance = $this->debtPrincipal;
foreach ($this->debtRepayments as $repayment) {
if($balance > $repayment->getTotalAmount()) {
$totalAmount = MathFuncs::round_up($repayment->getTotalAmount(), 2);
$interestAmount = MathFuncs::round($repayment->getInterestAmount(), 2);
$principalAmount = MathFuncs::round($totalAmount - $interestAmount, 2);
}
else {
$interestAmount = MathFuncs::round($repayment->getInterestAmount(), 2);
$totalAmount = MathFuncs::round(MathFuncs::add($balance, $interestAmount), 2);
$principalAmount = MathFuncs::round($balance, 2);
}
$beginningBalance = $balance;
$endingBalance = MathFuncs::sub($balance, $principalAmount);
$balance = MathFuncs::sub($balance, $principalAmount);
if($balance > $repayment->getTotalAmount()) {
$totalAmount = MathFuncs::roundUp($repayment->getTotalAmount(), 2);
$interestAmount = MathFuncs::round($repayment->getInterestAmount(), 2);
$principalAmount = MathFuncs::round($totalAmount - $interestAmount, 2);
}
else {
$interestAmount = MathFuncs::round($repayment->getInterestAmount(), 2);
$totalAmount = MathFuncs::round(MathFuncs::add($balance, $interestAmount), 2);
$principalAmount = MathFuncs::round($balance, 2);
}

$beginningBalance = $balance;
$endingBalance = MathFuncs::sub($balance, $principalAmount);
$balance = MathFuncs::sub($balance, $principalAmount);

$repayments[$i++] = [
"beginningBalance" => MathFuncs::round($beginningBalance, 2),
"totalAmount" => $totalAmount,
"beginningBalance" => MathFuncs::round($beginningBalance, 2),
"totalAmount" => $totalAmount,
"principalAmount" => $principalAmount,
"interestAmount" => $interestAmount,
"endingBalance" => MathFuncs::round($endingBalance, 2)
Expand Down
4 changes: 2 additions & 2 deletions src/Utils/MathFuncs.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public static function round($roundedNumber, $precision = 2)
* @param $precision
* @return string
*/
public static function round_up($roundedNumber, $precision = 2)
public static function roundUp($roundedNumber, $precision = 2)
{
return (string)ceil((float)$roundedNumber * pow(10, $precision)) / pow(10, $precision);
}
Expand All @@ -95,7 +95,7 @@ public static function round_up($roundedNumber, $precision = 2)
* @param $precision
* @return string
*/
public static function round_down($roundedNumber, $precision = 2)
public static function roundDown($roundedNumber, $precision = 2)
{
return (string)floor((float)$roundedNumber * pow(10, $precision)) / pow(10, $precision);
}
Expand Down
27 changes: 27 additions & 0 deletions tests/DebtAmortizatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,33 @@ private function processArray(array $resultArray)
$this->assertEquals("8686.63", round($repayments[6]["principalAmount"], 2));
$this->assertEquals("1042.4", round($repayments[6]["interestAmount"], 2));
$this->assertEquals($INDIVIDUAL_REPAYMENT, round($repayments[6]["totalAmount"], 2));

$repaymentsRounded = $resultArray["debtRepaymentsRounded"];

$INDIVIDUAL_REPAYMENT = "9729.03";
$this->assertEquals("4929.03", round($repaymentsRounded[1]["principalAmount"], 2));
$this->assertEquals("4800.00", round($repaymentsRounded[1]["interestAmount"], 2));
$this->assertEquals($INDIVIDUAL_REPAYMENT, round($repaymentsRounded[1]["totalAmount"], 2));

$this->assertEquals("5520.51", round($repaymentsRounded[2]["principalAmount"], 2));
$this->assertEquals("4208.52", round($repaymentsRounded[2]["interestAmount"], 2));
$this->assertEquals($INDIVIDUAL_REPAYMENT, round($repaymentsRounded[2]["totalAmount"], 2));

$this->assertEquals("6182.97", round($repaymentsRounded[3]["principalAmount"], 2));
$this->assertEquals("3546.06", round($repaymentsRounded[3]["interestAmount"], 2));
$this->assertEquals($INDIVIDUAL_REPAYMENT, round($repaymentsRounded[3]["totalAmount"], 2));

$this->assertEquals("6924.93", round($repaymentsRounded[4]["principalAmount"], 2));
$this->assertEquals("2804.10", round($repaymentsRounded[4]["interestAmount"], 2));
$this->assertEquals($INDIVIDUAL_REPAYMENT, round($repaymentsRounded[4]["totalAmount"], 2));

$this->assertEquals("7755.92", round($repaymentsRounded[5]["principalAmount"], 2));
$this->assertEquals("1973.11", round($repaymentsRounded[5]["interestAmount"], 2));
$this->assertEquals($INDIVIDUAL_REPAYMENT, round($repaymentsRounded[5]["totalAmount"], 2));

$this->assertEquals("8686.63", round($repaymentsRounded[6]["principalAmount"], 2));
$this->assertEquals("1042.4", round($repaymentsRounded[6]["interestAmount"], 2));
$this->assertEquals($INDIVIDUAL_REPAYMENT, round($repaymentsRounded[6]["totalAmount"], 2));
}

/**
Expand Down
10 changes: 10 additions & 0 deletions tests/MathFuncsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,14 @@ public function testRound()
{
$this->assertEquals("3.68", MathFuncs::round(3.675624));
}

public function testRoundUp()
{
$this->assertEquals("3.69", MathFuncs::round(3.675624));
}

public function testRoundDown()
{
$this->assertEquals("3.67", MathFuncs::round(3.675624));
}
}