1

Can anyone provide an alternative answer to my variable summations without it being a floating point decimal result with the possibility of displaying the final result as in an interger value. Whole numbers to be exact.

With every variable passing results in decimal notation, the final value is larger than the actual pen to paper I get using calculator.

This yeld s echo $terresulting in float value 43402777.777778. It should be more in the lines of 42,849,000 instead of the former, the latter value is my pen and paper. The problem is outputting final value to be non scientific notation. I need in straight interger value.

$tan = 100000;
$g = 30;
$epp = 12;
$days = 365;
$bpe = 4;
$pp = 300;

$apg = $tan / $g; 
$epg = $apg / $bpe;
$ppg = $epg / $epp;
$ypr = $ppg * $pp;
$tep = $ppg * $g;
$ter = $ypr * $tep;

echo $ter;
CodyA
  • 57
  • 5

1 Answers1

2

As is typical in computer science, floating-point arithmetic comes with its own set of problems.

Libraries are generally available for more precision. PHP offers the BC Math functions.

Your example can be altered to use this...

$tan = 100000;
$g = 30;
$epp = 12;
$days = 365;
$bpe = 4;
$pp = 300;

$apg = bcdiv($tan, $g); 
$epg = bcdiv($apg, $bpe);
$ppg = bcdiv($epg, $epp);
$ypr = bcmul($ppg, $pp);
$tep = bcmul($ppg, $g);
$ter = bcmul($ypr, $tep);
echo '$ter: ', $ter, PHP_EOL; // 42849000
echo 'Formatted: ', number_format($ter), PHP_EOL; // 42,849,000

Demo ~ https://3v4l.org/vlRaS

Phil
  • 157,677
  • 23
  • 242
  • 245