-
Notifications
You must be signed in to change notification settings - Fork 0
/
num.js
33 lines (27 loc) · 1.1 KB
/
num.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
33
// Math.ceil to specified decimal places (2nd argument, default 2)
function ceil2(value, places = 2) {
const multiplier = Number((1).toString().padEnd((places + 1), 0));
return Math.ceil(value * multiplier + Number.EPSILON) / multiplier;
}
// Math.floor to specified decimal places (2nd argument, default 2)
function floor2(value, places = 2) {
const multiplier = Number((1).toString().padEnd((places + 1), 0));
return Math.floor(value * multiplier + Number.EPSILON) / multiplier;
}
// Math.round to specified decimal places (2nd argument, default 2)
function round2(value, places = 2) {
const multiplier = Number((1).toString().padEnd((places + 1), 0));
return Math.round(value * multiplier + Number.EPSILON) / multiplier;
}
// For use with ADC sensor stats (5V sensors)
function ok2minmax(value) {
if (value < 0.9) return false;
if (value > 4.6) return false;
return true;
}
module.exports = {
ceil2 : (value, places = 2) => ceil2(value, places),
floor2 : (value, places = 2) => floor2(value, places),
round2 : (value, places = 2) => round2(value, places),
ok2minmax : (value) => ok2minmax(value),
};