-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
72 lines (59 loc) · 1.3 KB
/
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
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
const Abbreviated = {
/**
* @param {Number} value
*
* @returns {number}
*/
length(value) {
let len = 0;
while (value > 1) {
len += 1;
value /= 10;
}
return len;
},
/**
* @param {Number} value
*
* @returns {String}
*/
get(value) {
const len = this.length(value);
if (len <= 3) return value;
if (len > 3 && len <= 6) {
return `${value.toString().substr(0, len - 3)}K`;
}
if (len > 6 && len <= 9) {
return `${value.toString().substr(0, len - 6)}M`;
}
if (len > 9 && len <= 12) {
return `${value.toString().substr(0, len - 9)}B`;
}
if (len > 12 && len <= 15) {
return `${value.toString().substr(0, len - 12)}T`;
}
if (len > 15 && len <= 18) {
return `${value.toString().substr(0, len - 15)}q`;
}
if (len > 18 && len <= 21) {
return `${value.toString().substr(0, len - 18)}Q`;
}
return value;
},
/**
* @param {Number} amount
* @param {Number} len
*
* @returns {number}
*/
fixed(amount, len = 2) {
if(len>0){
let x = parseFloat(amount).toFixed(len);
let [a, b] = x.split(".");
b = b.substr(0, len);
return parseFloat(`${a}.${b}`);
}
return parseInt(amount);
}
};
module.exports = { Abbreviated };