-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnumscale.js
61 lines (48 loc) · 1.58 KB
/
numscale.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
NumScale = {
// Input should be an object with members 'value', 'maxLen', 'powerOf'
scale: function(input) {
var num = input.value,
result = '',
suffixPowerDecimal = ['', 'K', 'M', 'B', 'T', 'Q'],
suffixPowerBinary = ['', 'K', 'M', 'G', 'T', 'P'],
suffix = null,
index = 0, save = 0, divisor,
powerOf = input.powerOf || 10,
maxLen = input.maxLen || 20;
if (powerOf == 10) {
divisor = 1000;
suffix = suffixPowerDecimal;
} else if (powerOf == 2) {
divisor = 1024;
suffix = suffixPowerBinary;
} else throw new Error('invalid powerOf argument');
if (input.value < Number.MIN_SAFE_INTEGER || input.value > Number.MAX_SAFE_INTEGER) throw new Error('Number not within safe range');
while (num >= divisor) {
save = num;
num = num / divisor;
index++;
}
// Allow for suffix character(s)
maxLen -= suffix[index].length;
result = num.toString();
for (var p = result.length; result.length > maxLen; p--) {
if (p < 1) return ('Err');
result = num.toPrecision(p);
}
return (result + suffix[index]);
},
// Pad the left side of a string with spaces until it reaches 'len'
padl: function(str, len) {
var rval = '', i;
for (i = 0; i < len - str.length; i++) rval += ' ';
rval += str;
return (rval);
},
// Pad the right side of a string with spaces until it reaches 'len'
padr: function(str, len) {
var rval = '', i;
rval += str;
for (i = 0; i < len - str.length; i++) rval += ' ';
return (rval);
}
};