JavaScript如何在将数字相加时移除小数?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (48)

有一个脚本,它将4个数字相加在一起,但是JS没有考虑到最初的小数位

/初始输入表格

<form class="weight_form" data-ajax="false" method="post">
<input type="text" class="input_save" placeholder="save as.."/>
<input type="text" id="lf" placeholder="enter LF lbs" value="" onkeyup="myFunction()"/>
<input type="text" id="rf" placeholder="enter RF lbs" value="" onkeyup="myFunction()"/>
<input type="text" id="lr" placeholder="enter LR lbs" value="" onkeyup="myFunction()"/>
<input type="text" id="rr" placeholder="enter RR lbs" value="" onkeyup="myFunction()"/>
<input type="submit" id="sub_save" value="save"/>

<div id="bottom_section">
    <input type="text" id="total"  value=""/>
    <input type="text" id="cross"  value=""/>
    <input type="text" id="ls"  value=""/>
    <input type="text" id="bite"  value=""/>    
</div>

<script>
function myFunction() {
var lf = document.getElementById("lf");
var rf = document.getElementById("rf");
var lr = document.getElementById("lr");
var rr = document.getElementById("rr");

var lf = parseInt(lf.value);
var rf = parseInt(rf.value);
var lr = parseInt(lr.value);
var rr = parseInt(rr.value);

  var lf_1 = lf*1;
  var rf_1 = rf*1;
  var lr_1 = lr*1;
  var rr_1 = rr*1;

  var total = lf_1 + rf_1 + lr_1 + rr_1;
  var cross = rf_1 + lr_1 / total;
  var ls = lf_1 + lr_1 / total;
  var bite = lr_1 - rr_1;


$('#total').val(total);
$('#cross').val(Math.round(cross));
$('#ls').val(Math.round(ls));
$('#bite').val(bite);
};
</script>
提问于
用户回答回答于

要处理表单中的浮点数,在从元素中解析值时使用而不是,由于你使用的是jQuery,因此可以myFunction通过选择字段并在一行中解析其值来简化:

var lf = parseFloat($("#lf").val());
var rf = parseFloat($("#rf").val());
var lr = parseFloat($("#lr").val());
var rr = parseFloat($("#rr").val());

例:

function myFunction() {
  var lf = parseFloat($("#lf").val());
  var rf = parseFloat($("#rf").val());
  var lr = parseFloat($("#lr").val());
  var rr = parseFloat($("#rr").val());

  var lf_1 = lf * 1;
  var rf_1 = rf * 1;
  var lr_1 = lr * 1;
  var rr_1 = rr * 1;

  var total = lf_1 + rf_1 + lr_1 + rr_1;
  var cross = rf_1 + lr_1 / total;
  var ls = lf_1 + lr_1 / total;
  var bite = lr_1 - rr_1;

  $('#total').val(total);
  $('#cross').val(Math.round(cross));
  $('#ls').val(Math.round(ls));
  $('#bite').val(bite);
};
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<form class="weight_form" data-ajax="false" method="post">
  <input type="text" class="input_save" placeholder="save as.." />
  <input type="text" id="lf" placeholder="enter LF lbs" value="" onkeyup="myFunction()" />
  <input type="text" id="rf" placeholder="enter RF lbs" value="" onkeyup="myFunction()" />
  <input type="text" id="lr" placeholder="enter LR lbs" value="" onkeyup="myFunction()" />
  <input type="text" id="rr" placeholder="enter RR lbs" value="" onkeyup="myFunction()" />
  <input type="submit" id="sub_save" value="save" />
  <div id="bottom_section">
    <input type="text" id="total" value="" />
    <input type="text" id="cross" value="" />
    <input type="text" id="ls" value="" />
    <input type="text" id="bite" value="" />
  </div>
</form>

扫码关注云+社区

领取腾讯云代金券