Reputation: 45
I made a calculation in Excel and am trying to use it in PHP. Unfortunately, I do not get the correct total when I'm using the function as I wrote it.
This is the calculation as I am using it in Excel :
2500 / (( 0.25 / 100 ) / ( 1 - ( ( 1 + ( 0.25 / 100 )) ^ -360)))
The function I made for calculating in PHP:
function calculatesum($income, $interestmonthly, $months){
return $income / (( $interestmonthly / 100 ) / ( 1 - ( ( 1 + ( $interestmonthly / 100 ) ) ^ -$months)));
}
The function in PHP returns: '360000000' . But this should be '592.973,4538' .
I'm wondering what I'm doing wrong. Any tips will be welcome!
Already looked into the 'pow' functions, but still getting the same outcome.
Upvotes: 3
Views: 107
Reputation: 118
Use this. PHP does not raise power using ^. You can raise power of a value using
**
For example,
2**5 will be equal to 2^5
Hope this helps.
function calculatesum($income, $interestmonthly, $months){
return $income / (( $interestmonthly / 100 ) / ( 1 - ( ( 1 + ( $interestmonthly / 100 ) ) ** -$months)));
}
Upvotes: 1
Reputation: 49
PHP doesn't consider ^ as an operator, instead it uses another function pow. enter link description here I think that is all you have to change to get the value you are looking for.
function calculatesum($income, $interestmonthly, $months){
$n1=( $interestmonthly / 100 );
$n2= ( 1 + ( $interestmonthly / 100 ) );
$r1= pow($n2,-$months);
$r2=$n1 / ( 1 - $r1);
return $income /($n1 /$r2);
}
Upvotes: 0
Reputation: 8865
Excel uses ^
for calculating the power of a number. PHP uses pow($number, $exponent)
for that purpose.
function calculatesum($income, $interestmonthly, $months){
return $income / (( $interestmonthly / 100 ) / ( 1 - ( pow(( 1 + ( $interestmonthly / 100 ) ), -$months))));
}
echo calculatesum(2500,0.25,360);
Output:
592973.4537607
See http://php.net/manual/en/function.pow.php for documentation and examples.
Upvotes: 4