0

Possible Duplicate:
Elegant workaround for JavaScript floating point number problem

var sum = 0;
$('[id$=percentOfTotalEditor]').each(
    function () {
        var igEditor = $find(this.id);
        var value = igEditor.get_value();
        sum += value;
    }
);

Then you'll see the magic:

sum == 1.1, value == 0.1. But after sum += value, sum == 1.2000000000000001

WTF!?

Community
  • 1
  • 1
Sergey Metlov
  • 25,747
  • 28
  • 93
  • 153

2 Answers2

0

Numbers in javascript are stored as floating points - there will be some degree of imprecision - please see linked article, or search this site for floating point arithmetic for many explanations.

Paddy
  • 33,309
  • 15
  • 79
  • 114
0

Yes, this question is a duplicate as Paddy has suggested. Still, here's another Javascript library that will allow you to work with precise decimal numbers (up to a point): http://blog.brewsession.com/2008/06/28/problematic-decimal-arithmetic-in-javascript/

Bobby Eickhoff
  • 2,585
  • 2
  • 23
  • 22