1 |
|
2 |
|
3 |
|
4 | 'use strict';
|
5 |
|
6 | module.exports = class MathHelper {
|
7 |
|
8 | static random (min, max) {
|
9 | return Math.floor(Math.random() * (max - min + 1)) + min;
|
10 | }
|
11 |
|
12 | static round () {
|
13 | return this.adjustDecimal('round', ...arguments);
|
14 | }
|
15 |
|
16 | static ceil () {
|
17 | return this.adjustDecimal('ceil', ...arguments);
|
18 | }
|
19 |
|
20 | static floor () {
|
21 | return this.adjustDecimal('floor', ...arguments);
|
22 | }
|
23 |
|
24 | static adjustDecimal (type, value, precision) {
|
25 | if (!precision) {
|
26 | return Math[type](value);
|
27 | }
|
28 | if (isNaN(value) || !Number.isInteger(precision)) {
|
29 | return NaN;
|
30 | }
|
31 | value = value.toString().split('e');
|
32 | value = Math[type](+(value[0] + 'e' + (value[1] ? (+value[1] + precision) : precision)));
|
33 | value = value.toString().split('e');
|
34 | return +(value[0] + 'e' + (value[1] ? (+value[1] - precision) : -precision));
|
35 | }
|
36 | }; |
\ | No newline at end of file |