Skip to content

Instantly share code, notes, and snippets.

@nicolascine
Last active January 18, 2019 19:41
Show Gist options
  • Save nicolascine/b2ae04d218035fd6f5133911c1ba028d to your computer and use it in GitHub Desktop.
Save nicolascine/b2ae04d218035fd6f5133911c1ba028d to your computer and use it in GitHub Desktop.
https://stackoverflow.com/a/35965451
function truncate (num, places) {
return Math.trunc(num * Math.pow(10, places)) / Math.pow(10, places);
}
Then call it with:
truncate(3.5636232, 2); // returns 3.56
truncate(5.4332312, 3); // returns 5.433
truncate(25.463214, 4); // returns 25.4632
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment