Jquery计算 - 小数

问题描述 投票:0回答:1

我已经构建了一个表单,它总计,然后是另一个表单,总共需要4%,然后给出最终值。

我的问题是我没有收到计算的小数位数,所以例如如果总价格是500英镑而4%的删除是52.10美元,我的总价值是53,而不是52.10。

的jsfiddle:https://jsfiddle.net/znc93w90/1/

jQuery(document).ready(function(){
  var insOn = 15;
  var insOff = 0;
  var insuranceCover = 0;
  var qtyy=jQuery("#wpforms-2562-field_5");
  var completetotal=jQuery("#totaltwo"); 

  qtyy.keyup(function(){
    //NewValue = 4% of the total
    var newValue = parseInt(completetotal.val()) * 0.04;  
    //FinalValue = Total - 4% - 25
    var finalValue = parseInt(completetotal.val()) - parseInt(newValue) - 25;  


    //Spit the totals out
    jQuery('#totalfinal').text(finalValue.toFixed(2));
    jQuery('#testing').text(newValue.toFixed(2));
    jQuery('#testingtwo').val(finalValue.toFixed(2));
  });
});
javascript jquery math calculation
1个回答
0
投票

只是不要将你的数字解析为Int:

    //NewValue = 4% of the total
    var newValue = completetotal.val() * 0.04;  
    //FinalValue = Total - 4% - 25
    var finalValue = completetotal.val() - newValue - 25;  
© www.soinside.com 2019 - 2024. All rights reserved.