Skip to content

Instantly share code, notes, and snippets.

@joeynimu
Created February 15, 2016 08:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joeynimu/2a67bdf3a8a984701aca to your computer and use it in GitHub Desktop.
Save joeynimu/2a67bdf3a8a984701aca to your computer and use it in GitHub Desktop.
/*
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