Last active
April 14, 2021 07:58
-
-
Save dsetzer/f4ef26c84120f327103ee214510a9de6 to your computer and use it in GitHub Desktop.
Rounding with precision
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
// Arrow functions | |
const round = (num, prec) => (prec = 10 ** prec, Math.round(num * prec) / prec); | |
const roundUp = (num, prec) => (prec = 10 ** prec, Math.ceil(num * prec) / prec); | |
const roundDown = (num, prec) => (prec = 10 ** prec, Math.floor(num * prec) / prec); | |
// Classic functions | |
function round(num, prec) { | |
prec = Math.pow(10, prec); | |
return Math.round(num * prec) / prec; | |
} | |
function roundUp(num, prec) { | |
prec = Math.pow(10, prec); | |
return Math.ceil(num * prec) / prec; | |
} | |
function roundDown(num, prec) { | |
prec = Math.pow(10, prec); | |
return Math.floor(num * prec) / prec; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment