title: Why does adding two decimals in Javascript produce a wrong result?
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/588004/is-javascripts-math-broken">Is JavaScript&rsquo;s Math broken?</a>  </p>
  </blockquote>
  
  
  
  <p>Why does JS screw up this simple math?</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>console.log(.1 + .2)  // 0.3000000000000004
  console.log(.3 + .6)  // 0.8999999999999999</code></pre>
  </div>
  </div>
  </p>
  
  <p>The first example is greater than the correct result, while the second is less. ???!! How do you fix this? Do you have to always convert decimals into integers before performing operations? Do I only have to worry about adding (* and / don't appear to have the same problem in my tests)?</p>
  
  <p>I've looked in a lot of places for answers. Some tutorials (like shopping cart forms) pretend the problem doesn't exist and just add values together. Gurus provide complex routines for various math functions or mention JS "does a poor job" in passing, but I have yet to see an explanation.</p>