如何使用jquery将十进制数转换为分数

时间:2013-09-19 05:38:32

标签: javascript jquery

我想将十进制数5.5转换为5 1/2。我怎样才能做到这一点? 我需要检查号码。如果是5.5则将其转换为5 1/2。 请指教。

2 个答案:

答案 0 :(得分:1)

使用分数库和vanilla framework而不是jQuery,我想这样的东西会起作用:

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);

示例:http://jsfiddle.net/QwTPY/

答案 1 :(得分:0)

尝试https://github.com/ekg/fraction.js或maths.js。 Fraction.js是为了处理分数而构建的。

相关问题