Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP calculation function

Tags:

php

excel

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.

like image 284
0611nl Avatar asked Dec 02 '25 07:12

0611nl


1 Answers

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.

like image 182
maxhb Avatar answered Dec 04 '25 21:12

maxhb