我在下面有这些代码来计算最终得分。但是当我在计算器中检查时结果是不同的。我可以知道为什么吗?
守则:
function calculate() {
var a = $("#sc1").val();
var b = $("#sc2").val();
var c = $("#sc3").val();
scavg = (parseInt(a) + parseInt(b) + parseInt(c)) /3;
scavg = scavg.toFixed(2);
$("#scavg").val(scavg);
var d = $("#scavg").val();
var e = $("#np").val();
f = (parseInt(e)*3 + parseInt(d)*7) /10
$("input[name=na]").val(f);
}

答案 0 :(得分:0)
parseInt()
返回整数的整数。如果要向hundreth(scavg
)显示十进制数字,则.toFixed(2)
的格式为浮点数,parseFloat()
是您需要使用的。
function calculate() {
var a = $("#sc1").val();
var b = $("#sc2").val();
var c = $("#sc3").val();
scavg = (parseFloat(a) + parseFloat(b) + parseFloat(c)) / 3;
scavg = scavg.toFixed(2);
$("#scavg").val(scavg);
var d = $("#scavg").val();
var e = $("#np").val();
var f = (parseFloat(e) * 3 + parseFloat(d) * 7) / 10
$("input[name=na]").val(f);
}
$('input').on('input', calculate);

input {
font: inherit;
display: block
}

<input id='sc1' type='number' step="0.01" placeholder='SC1'>
<input id='sc2' type='number' step="0.01" placeholder='SC2'>
<input id='sc3' type='number' step="0.01" placeholder='SC3'>
<input id='scavg' type='number' step="0.01" placeholder='scAvg'>
<input id='np' type='number' step="0.01" placeholder='NP'>
<input name='na' type='number' step="0.01" placeholder='NA'>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
&#13;