Last Updated: January 05, 2018
· szymonkor

Floating point numbers in JS

I believe I could have titled the post "0.1+0.2=0.30000000000000004" because inner jokes get more clicks!

I read an interesting article on floating point numbers (http://blog.chewxy.com/2014/02/24/what-every-javascript-developer-should-know-about-floating-point-numbers/). Let's skip the boring math and focus on working knowledge.


  • Keep in mind that JS has only one numerical type (typeof 4 = "number")
  • use external libraries like BigDecimal.js, Math.js, Sinful.js


  • toFixed and toPrecision return strings. Use them only for display purposes
  • awesome advice: “expect floating point rounding errors, and duct tape around it”. The advice then follows on to say – if you expect 0.1 to be 0.10000000000000001 then work as if you’re working with 0.10000000000000001 all the time. I mean, wtf is with that kind of ridiculous advice??! Sorry, but that’s plain dumb.

That's all, folks!