Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

PHP subtraction problem

  • 22-02-2011 3:07pm
    #1
    Registered Users, Registered Users 2 Posts: 462 ✭✭


    Im getting a value of -5.8207660913467E-13 instead of 0 when I subtract two values. The two values are calculated values monetary but when echoed to screen seem to be the exact same. Ive tried converting them to cent and subtracting but with the same result. There must be a very slight difference in them but Im not sure how to resolve this. Any ideas what Im doing wrong?


Comments

  • Closed Accounts Posts: 1,150 ✭✭✭Ross


    How are you performing the subtraction? Posting the code you're using would help.

    Have a read of How to deal with strange rounding of floats in PHP.

    You probably want to use round.


  • Registered Users, Registered Users 2 Posts: 462 ✭✭lunacyfoundme


    Ill have a look at that thanks. Got around the issue for the minute by converting the decimalised monetary amount to cents and using (int) cast to make sure the cent amount is a whole number. Not sure if its loses a cent here or there but its a general report so it doesnt have to that accurate.

    Thanks.


  • Closed Accounts Posts: 2,046 ✭✭✭democrates


    I'd use round() first, (int) casting rounds down.


    Also be careful of the rounding error window, eg:

    107 * .21 = 22.47 rounds to 23
    107 * .79 = 84.53 rounds to 85

    so the sum of the parts is now greater than the whole...


Advertisement