convert decimal number to fraction in javascript or closest fraction

后端 未结 10 1596
温柔的废话
温柔的废话 2020-12-05 16:12

So i want to be able to convert any decimal number into fraction. In both forms such as one without remainder like this: 3/5 or with remainder: 3 1/4

10条回答
  •  猫巷女王i
    2020-12-05 16:40

    I came up with this for 16ths

    function getfract(theNum){
        var input=theNum.toString();
        var whole = input.split(".")[0];
        var rem = input.split(".")[1] * .1;
        return(whole + " " + Math.round(rem * 16) + "/16");
    }
    

提交回复
热议问题