-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathindex.js
32 lines (24 loc) · 785 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
function round(method, number, precision) {
if (typeof number !== 'number') {
throw new TypeError('Expected value to be a number');
}
if (precision === Number.POSITIVE_INFINITY) {
return number;
}
if (!Number.isInteger(precision)) {
throw new TypeError('Expected precision to be an integer');
}
const isRoundingAndNegative = method === 'round' && number < 0;
if (isRoundingAndNegative) {
number = Math.abs(number);
}
const power = 10 ** precision;
let result = Math[method]((number * power).toPrecision(15)) / power;
if (isRoundingAndNegative) {
result = -result;
}
return result;
}
export const roundTo = round.bind(undefined, 'round');
export const roundToUp = round.bind(undefined, 'ceil');
export const roundToDown = round.bind(undefined, 'floor');