We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 07b1493 commit d56dd3eCopy full SHA for d56dd3e
166-fraction-to-recurring-decimal.js
@@ -0,0 +1,26 @@
1
+/**
2
+ * @param {number} numerator
3
+ * @param {number} denominator
4
+ * @return {string}
5
+ */
6
+const fractionToDecimal = function (numerator, denominator) {
7
+ if (numerator === 0) return '0'
8
+ let s = ''
9
+ if (Math.sign(numerator) !== Math.sign(denominator)) s += '-'
10
+ let n = Math.abs(numerator)
11
+ const d = Math.abs(denominator)
12
+ s += Math.floor(n / d)
13
+ n %= d
14
+ if (n === 0) return s
15
+ s += '.'
16
+ const map = {}
17
+ while (n !== 0) {
18
+ map[n] = s.length
19
+ n *= 10
20
21
22
+ const i = map[n] // repeat starting index
23
+ if (i != null) return `${s.slice(0, i)}(${s.slice(i)})`
24
+ }
25
+ return s
26
+}
0 commit comments