Last active
July 27, 2021 00:05
-
-
Save rolangom/91ac256fd7814784493b4f75009a4032 to your computer and use it in GitHub Desktop.
functions javascript round numbers
This file contains hidden or 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(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