0

Here's the direct line of code.

totalArr[0] = parseFloat(totalArr[0]) + ( parseFloat(item.proj_AvgBal().toFixed(2)) * factor )

factor can only ever be 1 or -1. When I step through I get to point where totalArr[0] = 107860680.92 and the item.proj_AvgBal.toFixed(2) evaluates to 271194769.04.

After the addition of those two numbers happens, I'm left with 379055449.96000004 in totalArr[0].

Can someone please explain why this is happening?

jgonyer
  • 121
  • 1
  • 10
  • It's actually a little complicated and has to do with how computer compute floating point numbers. [Here's one source explaining the problem.](http://0.30000000000000004.com/) – Mike Cluck May 05 '15 at 14:39
  • Also relevant: http://stackoverflow.com/questions/588004/is-floating-point-math-broken – Andrew Whitaker May 05 '15 at 14:39
  • 1
    The most practical similar question is probably [How to deal with floating point number precision in JavaScript?](http://stackoverflow.com/questions/1458633/how-to-deal-with-floating-point-number-precision-in-javascript) – apsillers May 05 '15 at 14:40
  • You are just scratching the surface - try `0.2 * 0.2` in console, and despair. – Robert Rossmann May 05 '15 at 14:41

0 Answers0