-
Notifications
You must be signed in to change notification settings - Fork 0
/
fractions.js
39 lines (36 loc) · 1.08 KB
/
fractions.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
/**
* Convert a decimal to a fraction or integer string.
*
* @param number Number to express.
* @return String integer/fraction representation of number.
*/
function decimalToFrac(number) {
var numberFrac = math.fraction(number);
if (numberFrac.d != 1) {
return katex.renderToString(sign(numberFrac.s) + "\\dfrac{" +
(numberFrac.n) + "}{" + numberFrac.d + "}");
} else {
return katex.renderToString("" + (numberFrac.s * numberFrac.n));
}
}
/**
* Convert specified fraction string to decimal.
*
* @param str A string containing the fraction to convert to a number.
* @return Decimal.
*/
function fracToDecimal(str) {
// Extract fraction from string
var fraction = math.fraction(str);
// Extract number from fraction object
return fraction.s * fraction.n/fraction.d;
}
/**
* Determine the sign of the specified number.
*
* @param number Number whose sign is to be obtained.
* @return Either - or empty space.
*/
function sign(number) {
return Math.sign(number).toString().replace(/\d/, '');
}