forked from shesek/move-decimal-point
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
47 lines (44 loc) · 1.04 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
// Generated by CoffeeScript 1.12.7
(function() {
var zeros;
module.exports = function(num, n, char) {
var frac, int, neg, ref;
if (char == null) {
char = '.';
}
if (n === 0) {
return num;
}
ref = ('' + num).split(char), int = ref[0], frac = ref[1];
int || (int = '0');
frac || (frac = '0');
neg = (int[0] === '-' ? '-' : '');
if (neg) {
int = int.slice(1);
}
if (n > 0) {
if (n > frac.length) {
frac += zeros(n - frac.length);
}
int += frac.slice(0, n);
frac = frac.slice(n);
} else {
n = n * -1;
if (n > int.length) {
int = (zeros(n - int.length)) + int;
}
frac = int.slice(n * -1) + frac;
int = int.slice(0, n * -1);
}
while (int[0] === '0') {
int = int.slice(1);
}
while (frac[frac.length - 1] === '0') {
frac = frac.slice(0, -1);
}
return neg + (int || '0') + (frac.length ? char + frac : '');
};
zeros = function(n) {
return new Array(n + 1).join('0');
};
}).call(this);