-
Notifications
You must be signed in to change notification settings - Fork 130
/
Fraction to Recurring Decimal.js
61 lines (52 loc) · 1.55 KB
/
Fraction to Recurring Decimal.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
/**
Given two integers representing the numerator and denominator of a fraction, return the fraction in string format.
If the fractional part is repeating, enclose the repeating part in parentheses.
For example,
Given numerator = 1, denominator = 2, return "0.5".
Given numerator = 2, denominator = 1, return "2".
Given numerator = 2, denominator = 3, return "0.(6)".
* @param {number} numerator
* @param {number} denominator
* @return {string}
*/
var fractionToDecimal = function(numerator, denominator) {
var rem,
quotient,
map = [],
collection = '',
index,
len
result = '';
if (numerator === 0) {
return '0';
}
if (denominator === 0) {
return '';
}
if ((numerator < 0 && denominator > 0) || (numerator > 0 && denominator < 0)) {
result += '-';
}
numerator = Math.abs(Number(numerator));
denominator = Math.abs(Number(denominator));
quotient = Math.floor(numerator / denominator);
result += quotient;
rem = (numerator % denominator) * 10;
if (rem === 0) {
return result;
}
result += '.';
while (rem !== 0) {
quotient = Math.floor(rem/denominator);
index = map.indexOf(rem);
if (index === -1) {
map.push(rem);
collection += quotient;
} else {
collection = collection.substr(0, index) + '(' + collection.substr(index) + ')';
break;
}
rem = (rem % denominator) * 10;
}
result += collection;
return result;
};