PDA

View Full Version : calculation in php

shaileshpatil
07-30-2008, 01: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

djm0219
07-30-2008, 01: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, 01: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, 02: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, 04:20 PM