Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8402935
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T22:12:35+00:00 2026-06-09T22:12:35+00:00

Possible Duplicate: PHP Math Precision Best practice for working with currency values in PHP?

  • 0

Possible Duplicate:
PHP Math Precision
Best practice for working with currency values in PHP?

I really hope someone can help. I’ve been hitting my head against a brick wall on this one.

Here’s the situation:
I have a checkout which calculates the subtotal after discount to be -£11.50. There is then £11.50 delivery cost to add which IMHO should equal £0.

However when the calculation is run it returns a float 2.8421709430404E-14

In my debug efforts I have done this:

var_dump(
    $build['total'], // float(-11.5)
    $build['delivery'], // float(11.5)
    (($build['total'])+($build['delivery'])) // float(2.8421709430404E-14)
);

However when I do a static calculation:

var_dump((-11.5 + 11.5)); // float(0)

Any ideas?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T22:12:37+00:00Added an answer on June 9, 2026 at 10:12 pm

    2.8421709430404E-14 is the closest to (but not equal to) zero number PHP can output.
    The reason it’s not exactly 0 lies in the definition of floating point numbers – they’re never fully precise.

    If you need to work with sensible information that also contains fraction points, I suggest you bring it to integer by multiplying to the fraction.

    So $100.54 becomes 10054. After all calculations are done you can then divide back to the fraction.

    Example:

    $build['total'] = -11.5;
    $build['delivery'] = 11.5;
    
    var_dump(
        $build['total'], // float(-11.5)
        $build['delivery'], // float(11.5)
        (int)($build['total']*1000)+(int)($build['delivery']*1000) // int(0)
        (int)(round($build['total'], 4)*1000)+(int)(round($build['delivery'], 4)*1000) // int(0)
    );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: PHP Math Precision The following php code outputs 7 but I expect
Possible Duplicate: Is JavaScript's Math broken? Adding fractions number yields different result in PHP
Possible Duplicate: PHP code is not being executed (i can see it on source
Possible Duplicate: PHP: Require() and Class Hierarchies Is this bad practice? Is there any
Possible Duplicate: PHP: How to remove specific element from an array? How can i
Possible Duplicate: PHP 2-way encryption: I need to store passwords that can be retrieved
Possible Duplicate: PHP method chaining? So I can remember seeing in some code examples
Possible Duplicate: PHP pagination class are there any PHP/Class libraries that i can utilize
Possible Duplicate: PHP 2-way encryption: I need to store passwords that can be retrieved
Possible Duplicate: [PHP] global in functions Is it bad programming (PHP) practice to access

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.