Skip to content

Instantly share code, notes, and snippets.

@BryceCicada
Created May 10, 2016 07:50
Show Gist options
  • Save BryceCicada/ba4bd5604769ab8d70f87722cf9d2f6e to your computer and use it in GitHub Desktop.
Save BryceCicada/ba4bd5604769ab8d70f87722cf9d2f6e to your computer and use it in GitHub Desktop.
How to round correctly in javascript
function round(num, precision) {
// Avoids all the funny rounding behaviour of javascript, eg. Math.round(1.0005, 3) !== 1.001
return +(Math.round(num + 'e+' + precision) + 'e-' + precision);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment