Last Updated: February 25, 2016
·
1.964K
· avnerner

In JS (0.1 + 0.2 != 0.3)

I find many new comers to JS to miss that on float calculations.

if precise calculation is needed, it sometimes usefully to try something like:

var a = 0.1;
var b= 0.2
var FACTOR = 1000;
var c = a * FACTOR ;
var d  = b * FACTOR ;
var sum = (c + d) / FACTOR ;

More details at:
http://javascript.info/tutorial/number-math#imprecise-calculations

Have a fresh tip? Share with Coderwall community!

Post
Post a tip
Related Tags
#javascript
Did you know?

JavaScript on a page runs with the privileges of that origin. One altered library, tag manager, or snippet can read inputs, change what people see, and call your APIs as the user.

Security insights from Cside