Vanilla 1 is no longer supported or maintained. If you need a copy, you can get it here.
HackerOne users: Testing against this community violates our program's Terms of Service and will result in your bounty being denied.

JavaScript bug: 2*2=4.000000003 ??

edited July 2007 in Vanilla 1.0 Help
Hi, I am note experienced in JS, and I think many of you are acquainted with the problem. When I perform simplest arythmetic operations, JS returns these little bugs like 3*4=11.99999999999 etc. For real life example, choose detailed mode, 6 pages, 6 hours delivery, and don't check the specific checkbox at this paper price estimator test page (JS gurus please don't laugh at me). And then check the checkbox and submit again. This is completely irrational and wrong; I know it's a JS bug. What do I do with it?


  • Floating-point math is quirky. Try doing all your calculations in integers, and then add a decimal point afterward (if necessary).
  • Is there a rounding function?
  • Math.round(25.9) //returns 26
    Math.round(25.2) //returns 25
    Math.round(-2.58) //returns -3
    Math.round(11.99999999999) //returns 12

    Posted: Wednesday, 11 July 2007 at 10:43AM

  • I thought there might have been a function that you could tell it how many decimal places to round the number to. Guess I was wrong. So I searched and found this:
    function roundNumber(num, dec) { var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec); return result; }
    Then use it like:
    var roundedNumber = roundNumber(annualPremium,2)
This discussion has been closed.