forked from egorFiNE/node-stock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ExtraNumber.js
79 lines (52 loc) · 1.3 KB
/
ExtraNumber.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
/**
This module exports only a single additional method for built-in <code>Number</code> object.
*/
// trick doc.js into thinking that we have declared the class.
/*
function Number() {
}
*/
/**
Will return human-readable price for given integer.
@return {String} human-readable price.
Example:
var price = 233700;
price.humanReadablePrice(); // "23.37"
*/
Number.prototype.humanReadablePrice = function() {
return (this/10000).toFixed(2);
};
/**
Will return zero-padded number.
@param {Integer} length pad length.
@return {String} human-readable zero-padded number.
Example:
var something = 9;
something.pad(2); // "09";
*/
Number.prototype.pad = function(length) {
var _num = this.toString();
if (_num.length > length) {
return _num;
}
return (new Array(length).join('0') + this).slice(-length);
};
/**
Will return number shorted to an order of magnitude with one-letter description.
@return {String} human-readable number
Example:
var something = 93000000;
something.humanReadableOrder(); // "9.3m";
*/
Number.prototype.humanReadableOrder = function() {
if (this>=1000000000) {
return (this/1000000000).toFixed(1)+'b';
}
if (this>=1000000) {
return (this/1000000).toFixed(1)+'m';
}
if (this>=1000) {
return (this/1000).toFixed(1)+'k';
}
return this.toString();
};