# CompCalc

### By pimteam on Feb 07, 2012

This class calculates compounded interest on investment, saving or loan.
It allows variable compounding (reinvesting) percentage. Here's a page where you can see the class used: http://sharkinvestor.com/investment-calculator/

``````<?php
/* This class calculates compounded interest on investment, saving or loan.
It allows variable compounding (reinvesting) percentage
To do: add different types of loan calculations.
Will be useful for all kind of financial calculators and apps.
Here's a page where you can see the class used: http://sharkinvestor.com/investment-calculator/ */
class CompCalc
{
// calculates compounded investment
// \$num_periods - required, number of compounding periods (usually years or months)
// \$investment - required, the initial investment amount
// \$interest - required, interest in % for each period
// \$reinvest_percentage - optional, now much from the return is reinvested
// @returns array of arrays - one array for each period:
// [period, principal value, amount withdrawn so far, total return so far, total ROI]
// the results should be read as "at the end of period X"
function inv_calculate(\$num_periods, \$investment, \$interest,
{
// validate
if(\$num_periods<=0) throw new Exception("Periods should be at least 1");
if(\$investment<=0) throw new Exception("Investment amount should be bigger than 0");
if(!is_numeric(\$interest)) throw new Exception("Interest should be a number");
if(!is_numeric(\$reinvest_percentage)) throw new Exception("Reinvest % should be a number");

// convert interest and reinvest_percentage to decimal numbers
\$roi=\$interest/100;
\$cp=\$reinvest_percentage/100;

// Start the calculations

// STEP 1 & 2:
// 1. When the user input specific number of periods, you just go thru them
// 2. for each period calculate: Profit = Principal (Invested amount) X (ROI / 100)
\$total_withdrawn=0;
\$results=array();

for(\$i = 1 ; \$i < (\$num_periods+1) ; \$i++ )
{
\$new_principal=!empty(\$new_principal)?\$new_principal:\$investment;
\$new_principal=round(\$new_principal,2);

\$profit = \$new_principal * \$roi;
\$profit=round(\$profit,2);

// STEP 3:
// 3. Then substract the withdrawn amount.
// This amount is: Withdrawn = Profit X (compounding percentage / 100)

// totoal return
\$total_withdrawn+=\$withdraw;

// total ROI
\$total_roi=(\$total_withdrawn/\$investment)*100;
\$total_roi=round(\$total_roi);

\$result=array("period"=>\$i, "investment_value"=>\$new_principal,
"return_for_the_period"=>\$withdraw, "total_return"=>\$total_withdrawn,
"total_roi"=>\$total_roi);
\$results[]=\$result;
}

return \$results;
}
}
?>
``````

pimteam   -  Aug 16, 2012

Thanks folks!

Sorasyn   -  Aug 16, 2012

Ha, I could have used this in Accounting last year.

Hawkee   -  Aug 16, 2012

Very nice. I appreciate your commenting.

pimteam   -  Jul 13, 2012

Sorry for the late reply (there was no email about getting a comment). Yeah, code is mine.

Hawkee   -  Mar 20, 2012

Did you write this yourself?

Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.