I started learning jquery, so I want to write a script to convert decimal fractions to ordinary ones.

While it turned out, and here is the code.

var number = 40.1250223; alert(number + " Вводимое число"); var remain = number % 1; var remainFixed = remain.toFixed(4); alert(remainFixed + " Сокращенная нецелая часть числа"); var factor = 10000; var numberFixed = number.toFixed(); alert(numberFixed + " Число до запятой"); var decimalNumber = remainFixed * factor; alert(decimalNumber + " Число после запятой"); var wholeNumber = numberFixed + decimalNumber; alert(wholeNumber + " Целое число"); var numerator = wholeNumber; var denominator = factor; alert(numerator + ' / ' + denominator + " Дробное число"); 

Further, as far as I can imagine, it is necessary to calculate the greatest common factor using a cycle and reduce the number, but I don’t understand how to realize it yet.

  • How to find a divider, or how to cut? - nick_n_a
  • how to find a divider. If you don't know how, how would you be looking for ...? What range does it lie in? If "vlob" then this number inter 2 and min (numerator, denominator) right? Loop over and find the number at which numerator% number and denominator% number will give zero at the same time. - nick_n_a February
  • @nick_n_a well, it’s logical that how to find a divider) to cut, you just need to divide it into it) - kost1k
  • Possible duplicate question: General numerical divider - nick_n_a
  • numberFixed you have not number , but string - Grundy

0