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

  • SEARCH
  • Home
  • 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 7944867
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T00:40:48+00:00 2026-06-04T00:40:48+00:00

In JavaScript this math operation returns: 1913397 / 13.054 = 146575.53240386088 but same operation

  • 0

In JavaScript this math operation returns:

1913397 / 13.054 =  146575.53240386088 

but same operation in php returns:

1913397 / 13.054 =  146575.53240386

I guess this is due to rounding.

How can I extend php’s precision?

  • 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-04T00:40:50+00:00Added an answer on June 4, 2026 at 12:40 am

    From an article I wrote for Authorize.Net:

    One plus one equals two, right? How about .2 plus 1.4 times 10? That equals 16, right? Not if you’re doing the math with PHP (or most other programming languages):

    echo floor((0.2 + 1.4) * 10); // Should be 16. But it's 15!
    

    This is due to how floating point numbers are handled internally. They are represented with a fixed number of decimal places and can result in numbers that do not add up quite like you expect. Internally our .2 plus 1.4 times 10 example computes to roughly 15.9999999998 or so. This kind of math is fine when working with numbers that do not have to be precise like percentages. But when working with money precision matters as a penny or a dollar missing here or there adds up quickly and no one likes being on the short end of any missing money.

    The BC Math Solution

    Fortunately PHP offers the BC Math extension which is "for arbitrary precision mathematics PHP offers the Binary Calculator which supports numbers of any size and precision, represented as strings." In other words, you can do precise math with monetary values using this extension. The BC Math extension contains functions that allow you to perform the most common operations with precision including addition, subtraction, multiplication, and division.

    A Better Example

    Here’s the same example as above but using the bcadd() function to do the math for us. It takes three parameters. The first two are the values we wish to add and the third is the number of decimal places we wish to be precise to. Since we’re working with money we’ll set the precision to be two decimal palces.

    echo floor(bcadd('0.2', '1.4', 2) * 10); // It's 16 like we would expect it to be.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Is JavaScript’s math broken? This seems really stupid, but when running this,
What is the JavaScript Math.Floor() equivalent in VBA?. this function should remove all the
This javascript code does not work in IE8, but works in Firefox and Google
This javascript will find all my selects and validate them but I want to
I have this bookmarklet: javascript:void(window.open(document.links[Math.floor(Math.random()*document.links.length)].href,'_blank')) It outputs random link from a document full of
I have this javascript: $('.foto').filter(function(index) { return index == Math.floor(Math.random() * 8) + 1;
What is the vb.net or c# equivalent of the following javascript? this.browserTime.value = Math.floor((new
Why this javascript working fine in Firefox but not in IE 6 and 7
Possible Duplicate: Is JavaScript's Math broken? It returns 5.074999999999999 Why is that? And what
I've got a problem concerning the javascript this keyword when used within a javascript

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.