如何在javascript中获得2位小数的百分比

时间:2016-12-14 07:58:19

标签: javascript

我希望获得最多2位小数的百分比不超过该值。我得到的输出总分为 26.666666666666668%

期望产出26.66%

if(pos >= questions.length){
        test.innerHTML = "<h2>You gave  "+correct+" CORRECT answers out of "+questions.length+" questions </h2><br/><h3>Total Score is "+correct/questions.length * 100 +"  %</h3>";
        _("test_status").innerHTML = "Quiz 1 Completed";
        pos = 0;
        correct = 0;
        return false;
    }

1 个答案:

答案 0 :(得分:0)

    <button onclick="myFunction()">Try it</button>

<p id="demo"></p>

<script>
function myFunction() {
    var num = 26.666666666666668;
    var n = num.toFixed(2)
    document.getElementById("demo").innerHTML = n;
}
</script>