Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

1. ## calculation in php

Hi

I am doing simple addition and subtraction in php.
CASE 1
PHP Code:
``` \$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

• How large are the numbers you are working with and are they integers or floats (doubles)? Some real examples would probably help also.

• 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.

• A simple

PHP Code:
``` \$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).

• Add brackets around \$var1 and \$var2, to follow order of operations.
PHP Code:
``` \$total = ( \$var1 - \$var2 ) - \$var3;  ```

• 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

•

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•