-
Notifications
You must be signed in to change notification settings - Fork 3
/
prototypes.js
87 lines (78 loc) · 2.23 KB
/
prototypes.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
if (!Number.prototype.$floor) {
Number.prototype.$floor = function (decimals) {
if (typeof decimals === 'undefined') {
decimals = 0;
}
return Math.floor(
this * Math.pow(10, decimals)
) / Math.pow(10, decimals);
};
}
if (!Number.prototype.$ceil) {
Number.prototype.$ceil = function (decimals) {
if (typeof decimals === 'undefined') {
decimals = 0;
}
return Math.ceil(
this * Math.pow(10, decimals)
) / Math.pow(10, decimals);
};
}
if (!Number.prototype.$round) {
Number.prototype.$round = function (decimals) {
if (typeof decimals === 'undefined') {
decimals = 0;
}
return Math.round(
this * Math.pow(10, decimals)
) / Math.pow(10, decimals);
};
}
if (!Number.prototype.$localize) {
Number.prototype.$localize = function (decimals) {
let options = {};
if (typeof decimals !== 'undefined') {
options.maximumFractionDigits = decimals;
}
return new Intl.NumberFormat(undefined, options).format(this);
};
}
if (!Number.prototype.$currency) {
Number.prototype.$currency = function (decimals) {
let options = {};
if (typeof decimals === 'undefined') {
decimals = 2;
}
options.maximumFractionDigits = decimals;
return new Intl.NumberFormat(undefined, options).format(this);
};
}
if (!Number.prototype.$min) {
Number.prototype.$min = function (limit) {
if (limit < this) return limit;
return this;
};
}
if (!Number.prototype.$max) {
Number.prototype.$max = function (limit) {
if (limit > this) return limit;
return this;
};
}
if (!Number.prototype.$highest) {
Number.prototype.$highest = function (limit) {
return this.$min(limit);
};
}
if (!Number.prototype.$lowest) {
Number.prototype.$lowest = function (limit) {
return this.$max(limit);
};
}
if (!Number.prototype.$limit) {
Number.prototype.$limit = function (minimum, maximum) {
if (minimum !== null && this < minimum) return minimum;
if (maximum !== null && this > maximum) return maximum;
return this;
};
}