Skip to content

Instantly share code, notes, and snippets.

@rolangom
Last active July 27, 2021 00:05
Show Gist options
  • Save rolangom/91ac256fd7814784493b4f75009a4032 to your computer and use it in GitHub Desktop.
Save rolangom/91ac256fd7814784493b4f75009a4032 to your computer and use it in GitHub Desktop.
functions javascript round numbers
function round(n, e) {
const f = e === 0 ? 1 : Math.pow(10, e);
return Math.round(n * f) / f;
}
// test examples
/*
> round(123.123, 2)
< 123.12
> round(123.123, 1)
< 123.1
> round(123.123, 0)
< 123
> round(123.123, 2)
< 123.12
> round(123.123, -2)
< 100
> round(123.123, -1)
< 120
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment