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.
Asked
Active
Viewed 1,694 times
0
-
Why would you need JQuery to do that? – MaVRoSCy Sep 19 '13 at 05:42
-
if you mean convert only 5.5 -> then try: if(parseFloat(.value).toFixed(1) == 5.5){return '5 1/2'} – Dart Sep 19 '13 at 06:19
2 Answers
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
-
It doesnt work for me.I need to write 5 1/2 if the variable contains 5.5. so conversion will not work here. – Neha Sep 19 '13 at 05:49
-
works for me: http://jsfiddle.net/QwTPY/ 5.5 => "5 1/2", 5.75 => "5 3/4" – gherkins Sep 19 '13 at 07:04
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