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 855299
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T08:03:17+00:00 2026-05-15T08:03:17+00:00

In php, we have number_format() . Passing it a value such as: number_format(3.00 *

  • 0

In php, we have number_format(). Passing it a value such as:

number_format(3.00 * 0.175, 2);

returns 0.53, which is what I would expect.

However, in JavaScript using toFixed()

var num = 3.00 * 0.175;
num.toFixed(2);

returns 0.52.

Ok, so perhaps toFixed is not what I want… Maybe something like this…

var num = 3.17 * 0.175;
var dec = 2;
Math.round( Math.round( num * Math.pow( 10, dec + 1 ) ) / Math.pow( 10, 1 ) ) / Math.pow(10,dec);

No, that doesn’t work either. It will return 0.56.

How can I get a number_format function in JavaScript that doesn’t give an incorrect answer?

Actually I did find an implementation of number_format for js, http://phpjs.org/functions/number_format, but it suffers from the same problem.

What is going on here with JavaScript rounding up? What am I missing?

  • 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-05-15T08:03:17+00:00Added an answer on May 15, 2026 at 8:03 am

    JavaScript does badly with floating point numbers (as do many other languages).

    When I run

    3.000 * 0.175
    

    In my browser, I get

    0.5249999999999999
    

    Which will not round up to 0.525 with Math.round. To circumvent this, you kind of have to multiply both sides until you get them to be integers (relatively easy, knowing some tricks help though).

    So to do this we can say something like this:

    function money_multiply (a, b) {
        var log_10 = function (c) { return Math.log(c) / Math.log(10); },
            ten_e  = function (d) { return Math.pow(10, d); },
            pow_10 = -Math.floor(Math.min(log_10(a), log_10(b))) + 1;
        return ((a * ten_e(pow_10)) * (b * ten_e(pow_10))) / ten_e(pow_10 * 2);
    }
    

    This may look kind of funky, but here’s some pseudo-code:

    get the lowest power of 10 of the arguments (with log(base 10))
    add 1 to make positive powers of ten (covert to integers)
    multiply
    divide by conversion factor (to get original quantities)
    

    Hope this is what you are looking for. Here’s a sample run:

    3.000 * 0.175
    0.5249999999999999
    
    money_multiply(3.000, 0.175);
    0.525
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a PHP function that takes a variable number of arguments (using func_num_args()
Using PHP.. Here is what I have.. I'm gonna explain the whole thing and
I have the following php code (getRout.php).. which response results in xml formats ...
I have some code for a PHP shopping cart which is as follow if($cart->itemcount
On a website I have a number of small PHP scripts to automate changes
I am trying to dial a phone number from php (i have a client
I have a php form that has a known number of columns (ex. top
I have a significant number of object libraries written for PHP 5.2.5, and I'm
I have a PHP array with numbers of ID's in it. These numbers are
Does PHP have a method of having auto-generated class variables? I think I've seen

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.