0

I want to convert the decimal number 5.5 to 5 1/2. How can i do that? I need to check for the number. If it is 5.5 then convert it to 5 1/2. Please advice.

Neha
  • 11
  • 2
  • 10

2 Answers2

2

Using the fraction library and the vanilla framework instead of jQuery, i suppose something like this would work:

https://github.com/ekg/fraction.js

var num = 5.5
, rounded = Math.floor(num)
, rest  = num - Math.floor(num);

var f = new Fraction(1, rest);
console.log(rounded + ' ' + f.numerator + '/' + f.denominator);

example: http://jsfiddle.net/QwTPY/

gherkins
  • 14,603
  • 6
  • 44
  • 70
0

Try https://github.com/ekg/fraction.js or maths.js. Fraction.js was built for the purpose of working with fractions.

Bazinga777
  • 5,140
  • 13
  • 53
  • 92