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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:44:30+00:00 2026-06-17T16:44:30+00:00

Possible Duplicate: Is JavaScript’s Floating-Point Math Broken? I have a strange mathematical problem during

  • 0

Possible Duplicate:
Is JavaScript’s Floating-Point Math Broken?

I have a strange mathematical problem during a multiplication in javascript.

$(parent).find('#id_deals-' + i + '-quantity').val()

result -> 10

$(parent).find('#id_deals-' + i + '-price').val()

result -> 3.99

Both above mulltiplied like this:

$(parent).find('#id_deals-' + i + '-price').val() * $(parent).find('#id_deals-' + i + '-quantity').val()

result –> 39.900000000000006

Why is this happening? and what can I do to limit the decimal places to 2 digits only?

Is it maybe because 10 has to be 10.0 ? But how do I convert my value to this format automatically before the actual multiplication?

Update:
According to syazdani’s answer, I have tried to implement bigdecimal as suggested:

It is not well documented, but I got it working like this:

function run(opts) {
    var bd = {"BigDecimal":BigDecimal, "BigInteger":BigInteger, "RoundingMode":RoundingMode};
    var result;
    var ops = {'*': "multiply", '/': "divide", '+': "add", '-': "subtract"};
    var a = new bd.BigDecimal("" + opts.a);
    var b = new bd.BigDecimal("" + opts.b);
    var op = ops[opts.op];
    if (op == "divide") {
        return a.divide(b, 300, bd.RoundingMode.HALF_UP());
    } else {
        return a[op].call(a, b);
    }
}


function multiply(a, b){
    return run({"a":a,"b":b,"op":"*"}); 
} 
  • 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-17T16:44:31+00:00Added an answer on June 17, 2026 at 4:44 pm

    If you are working with currency (as it seems that you are given the “price” id), you may be better served by using a so called Big Number library (such as this one: https://github.com/iriscouch/bigdecimal.js) for your math to control the math (round up vs round down, etc.). It takes a bit more work to get everything right, but it is worthwhile to avoid the Office Space math scenario.

    • 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 Floating-Point Math Broken? Note - I have read other posts
Possible Duplicate: Is JavaScript's Math broken? Strange result with floating point addition Some simple
Possible Duplicate: Is JavaScript’s Floating-Point Math Broken? In Javascript, 3 * 0.1 = 0.30000000000000004
Possible Duplicates: Is JavaScript's Math broken? Java floating point arithmetic I have the current
Possible Duplicate: Is JavaScript’s Floating-Point Math Broken? This is going to be a very
Possible Duplicate: Is JavaScript’s Floating-Point Math Broken? Rounding a float in JavaScript Why this
Possible Duplicate: Elegant workaround for JavaScript floating point number problem var sum = 0;
Possible Duplicate: Elegant workaround for JavaScript floating point number problem var sum = 0;
Possible Duplicate: Elegant workaround for JavaScript floating point number problem If I perform the
Possible Duplicate: Elegant workaround for JavaScript floating point number problem I would like to

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.