0

I have to do some calculation in decimals in my application using javascript but the result is unexpected.

The result of the below statement is 1.707 [as expected]

var total=1.17+0.237+0.3;

also when we add the numbers below, I am getting 0.7070000000000001 [unexpected]

var totalnew=0.17+0.237+0.3;

Also, the expression (0.1+0.2===0.3) returns false[unexpected], but (1.1+0.2===1.3) returns true[as expected] . Why is this happening and what is the workaround for this.

KanhuP2012
  • 407
  • 3
  • 9
  • 2
    possible duplicate of [Is floating point math broken?](http://stackoverflow.com/questions/588004/is-floating-point-math-broken) – A1rPun Nov 13 '14 at 12:45
  • Maybe this question helps? http://stackoverflow.com/questions/2876536/precise-financial-calculation-in-javascript-what-are-the-gotchas – Stefan Nov 13 '14 at 12:45
  • 1
    http://www.smbc-comics.com/index.php?db=comics&id=2999#comic – Thilo Nov 13 '14 at 12:47
  • Why total is coming correct but totalnew is coming not as expected. – KanhuP2012 Nov 13 '14 at 15:44

2 Answers2

3

Floating point is notoriously tricky. Basically it boils down to that there are an infinite amount of values between two real numbers, so it is impossible to represent them correctly in a computer.

If you want to print the number i suggest:

total.toFixed();

Which will always give you three decimal places. And when you want to check if two floats are the same you need to do something like this:

function nearlyEqual(a, b, epsilon) {
    var absA = Math.abs(a);
    var absB = Math.abs(b);
    var diff = Math.abs(a - b);
    var minNormal = 1.1754943508222875e-38;

    if (a == b) { // shortcut, handles infinities
        return true;
    } else if (a == 0 || b == 0 || diff < minNormal) {
        // a or b is zero or both are extremely close to it
        // relative error is less meaningful here
        return diff < (epsilon * minNormal);
    } else { // use relative error
        return diff / (absA + absB) < epsilon;
    }
}

nearlyEqual(0.1+0.2, 0.3, 0.0001);

As is suggested here How should I do floating point comparison?

Community
  • 1
  • 1
cptcactus
  • 212
  • 1
  • 5
0

This is because the values have no fixed precision. To resolve this, try implementing Math.Round.

Math.round((0.17 + 0.237 + 0.3) * 1e12) / 1e12
wblanks
  • 598
  • 10
  • 27