Created
May 10, 2016 07:50
-
-
Save BryceCicada/ba4bd5604769ab8d70f87722cf9d2f6e to your computer and use it in GitHub Desktop.
How to round correctly in javascript
This file contains 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
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