Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    Nov 2006
    Location
    INDIA
    Posts
    94
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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

    Thanks in advance
    -------------------------
    Shailesh Patil
    -------------------------

  • #2
    Senior Coder djm0219's Avatar
    Join Date
    Aug 2003
    Location
    Wake Forest, North Carolina
    Posts
    1,303
    Thanks
    4
    Thanked 204 Times in 201 Posts
    How large are the numbers you are working with and are they integers or floats (doubles)? Some real examples would probably help also.
    Dave .... HostMonster for all of your hosting needs

  • #3
    New Coder
    Join Date
    Nov 2006
    Location
    INDIA
    Posts
    94
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.
    -------------------------
    Shailesh Patil
    -------------------------

  • #4
    Senior Coder djm0219's Avatar
    Join Date
    Aug 2003
    Location
    Wake Forest, North Carolina
    Posts
    1,303
    Thanks
    4
    Thanked 204 Times in 201 Posts
    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).
    Dave .... HostMonster for all of your hosting needs

  • #5
    Regular Coder BWiz's Avatar
    Join Date
    Mar 2006
    Location
    Sol System
    Posts
    471
    Thanks
    7
    Thanked 21 Times in 21 Posts
    Add brackets around $var1 and $var2, to follow order of operations.
    PHP Code:
    $total = ( $var1 $var2 ) - $var3
    BWiz :: Happy Coding!
    2006
    2007 2008 2009
    2010 2011
    Irrational numbers make no sense.

  • #6
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    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
    •