Need coding help.

Discussion in 'PHP' started by spotonuk, Oct 25, 2008.

  1. #1
    hi

    pls help me out, if any know how to script for bank deposit (compound interest calculations.)


    thanks
     
    spotonuk, Oct 25, 2008 IP
  2. Pos1tron

    Pos1tron Peon

    Messages:
    95
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Two versions - one is fully commented, the other's a function - more easy to use:
    <?php
    
    /**
     *	The below is the deposit after
     *	Don't include a currency value, 
     *	ideally, though I've included a 
     *	function to make it work either 
     *	way with or without.
    **/
    $depositAfterPrecise = $depositBefore = floatval(94335.56);
    
    /**
     *	The below is the percentage interest rate
     *	3.2 means an interest rate of 3.2% per year
    **/
    $interestRate = 3.2;
    
    /**
     *	The below is the number of years of interest
     *	7 means 7 years, obviously 1 month is 1 divided 
     *	by 12 which is actually a recurring value.
    **/
    $interestYears = 7;
    
    /**
     *	This is the actual logic below
     *	There's no more configuration values
    **/
    $interestRateOverall = ($interestRate / 100) * $interestYears;
    $depositAfterPrecise += $depositBefore * $interestRateOverall;
    $depositAfter = round($depositAfterPrecise, 2);
    
    /**
     *	The below two lines just output the values 
     *	so you can check that it's working.
    **/
    echo $depositAfterPrecise . '<br />';
    echo $depositAfter . '<br />';
    
    /**
     *	$depositAfterPrecise in this case is 115466.72544
     *		This isn't rounded - you aren't usually interested 
     *		in such a precise value in this case.
     *	$depositAfter in this case is 115466.73
     *		This is a proper rounded currency value, with 2 
     *		decimal points only, as you'd usually use.
    **/
    
    ?>
    PHP:
    This next one's a function:
    <?php
    
    function compoundInterest($depositBefore, $interestRate, $interestYears) {
    	/**
    	 *	This is the actual logic below
    	**/
    	$depositAfterPrecise = $depositBefore;
    	$interestRateOverall = ($interestRate / 100) * $interestYears;
    	$depositAfterPrecise += $depositBefore * $interestRateOverall;
    	$depositAfter = round($depositAfterPrecise, 2);
    	
    	return $depositAfter;
    }
    
    /**
     *	The below is the deposit after
     *	Don't include a currency value, 
     *	ideally, though I've included a 
     *	function to make it work either 
     *	way with or without.
    **/
    $depositBefore = floatval(94335.56);
    
    /**
     *	The below is the percentage interest rate
     *	3.2 means an interest rate of 3.2% per year
    **/
    $interestRate = 3.2;
    
    /**
     *	The below is the number of years of interest
     *	7 means 7 years, obviously 1 month is 1 divided 
     *	by 12 which is actually a recurring value.
    **/
    $interestYears = 7;
    
    
    /**
     *	This line below runs the actual logic
    **/
    $depositAfter = compoundInterest($depositBefore, $interestRate, $interestYears);
    
    
    /**
     *	The below line just outputs the value 
     *	so you can check that it's working.
    **/
    echo $depositAfter . '<br />';
    
    /**
     *	$depositAfterPrecise isn't returned in this case.
     *	$depositAfter in this case is 115466.73
     *		This is a proper rounded currency value, with 2 
     *		decimal points only, as you'd usually use.
    **/
    
    ?>
    PHP:
     
    Pos1tron, Oct 25, 2008 IP
  3. spotonuk

    spotonuk Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Hi buddy

    Thanks for the help. I follow your script with some changes and its work. check this out and please reply back.

    Thanks
     

    Attached Files:

    • emi.php
      File size:
      4.4 KB
      Views:
      56
    spotonuk, Oct 27, 2008 IP