...

View Full Version : calculation in php



shaileshpatil
07-30-2008, 12:12 PM
Hi

I am doing simple addition and subtraction in php.
CASE 1


$lngAmountDifference = $lngInvoiceTotalAmountInclTax - $lngInvoiceTotalTaxAmount;
$lngAmountDifference = $lngAmountDifference - $lngSumOfItemAmount;
print 'DIFFERENCE ='.$lngAmountDifference.'<br>';


For some results its right and for some it prints 4.57775455E, even though the difference should be zero. Why this happens?
CASE 2
If i use (int)$var-(int)$var2, it is perfect, but as soon as bigger value appears, it ends up with same.
CASE 3
If i use (double)$var1-(double)$var2, still the results differ from all the cases mentioned above.

Plz help

Thanks in advance

djm0219
07-30-2008, 12:28 PM
How large are the numbers you are working with and are they integers or floats (doubles)? Some real examples would probably help also.

shaileshpatil
07-30-2008, 12:47 PM
For example
$var1 = 519607
$var 2= 82963
$var 3=436644

I want ($var1-$var2)-$var3

var1, var2 and var3 not necessarily be positive numbers but definitely not decimals.

djm0219
07-30-2008, 01:14 PM
A simple


$total = $var1 - $var2 - $var3;

should get you the results you expect unless the numbers grow too large (exceed the number of digits PHP supports for integer values).

BWiz
07-30-2008, 03:20 PM
Add brackets around $var1 and $var2, to follow order of operations.


$total = ( $var1 - $var2 ) - $var3;

oesxyl
07-30-2008, 03:39 PM
I guess the problem is not here. Look how did you compute $lngAmountDifference and $lngInvoiceTotalTaxAmount. More exactly look if you have any division '/' with a very small number or with the difference of to equal or almost equal numbers.

regards



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum