Skip to content

Instantly share code, notes, and snippets.

@hn3000
Created October 1, 2018 21:14
Show Gist options
  • Save hn3000/e784fdf5b0651776975af4a4bc732808 to your computer and use it in GitHub Desktop.
Save hn3000/e784fdf5b0651776975af4a4bc732808 to your computer and use it in GitHub Desktop.
function roundToDigits(x,n) {
let digits = Math.ceil(Math.log(x) / Math.log(10));
let scale = Math.pow(10, n - digits);
return Math.round(x * scale)/scale;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment