Created
February 15, 2016 08:43
-
-
Save joeynimu/2a67bdf3a8a984701aca to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
this is a work around adding numbers and taking care of those that are floats | |
for e.g | |
function add(x, y){ | |
return x + y; | |
} | |
add(0.3, 0.1); this will not equal to 0.4 for floats are treated differently see the below link | |
http://stackoverflow.com/questions/588004/is-floating-point-math-broken | |
*/ | |
function add(a, b, precision) { | |
var x = Math.pow(10, precision || 2); | |
return (Math.round(a * x) + Math.round(b * x)) / x; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment