6 function almostEq(a, b) {
7 return abs(a - b) <= 9.5367432e-7
10 //最大公约数 Greatest Common Divisor
12 if (almostEq(b, 0)) return a
16 function findPrecision(n) {
19 while (!almostEq(round(n * e) / e, n)) {
26 function num2fraction(num) {
27 if (num === 0 || num === '0') return '0'
29 if (typeof num === 'string') {
34 var precision = findPrecision(num) //精确度
35 var number = num * precision
36 var gcd = abs(GCD(number, precision))
39 var numerator = number / gcd
41 var denominator = precision / gcd
44 return round(numerator) + '/' + round(denominator)
47 module.exports = num2fraction