Skip to content

Instantly share code, notes, and snippets.

@semihkeskindev
Last active August 10, 2020 15:58
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 semihkeskindev/f276308b27bdb150d85f1226c6e00fa8 to your computer and use it in GitHub Desktop.
Save semihkeskindev/f276308b27bdb150d85f1226c6e00fa8 to your computer and use it in GitHub Desktop.
Javascript function for Round to any decimal place
function roundNumber(num, precision = 0) {
let decimalPlace = Math.pow(10, precision);
return Math.round((num + Number.EPSILON) * decimalPlace) / decimalPlace;
}
@semihkeskindev
Copy link
Author

If you wonder why i use "Number.EPSILON", thats why below:
https://stackoverflow.com/a/11832950/10956918

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment