2016-11-09 44 views
2

我正在研究一个需要“提示框”计算系统的项目。正如你在代码片段中看到的那样,它不能像我期望的那样工作。我怎么解决这个问题?parseFloat没有给出准确的值

$("select[name='tip']").on('change',function(){ 
 
    var thiz = $(this); 
 
    var content_credit = parseFloat($("#f_content_credit").text()); 
 
    var total_balance = parseFloat($("#f_total_balance").text()); 
 
    var tip = parseFloat(thiz.val()); 
 

 
    console.log(total_balance+"-"+content_credit+"-"+tip); 
 
    
 
    $("#f_after_confirm").text(total_balance-content_credit-tip); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<table class="table table-bordered table-hover"> 
 
    <tbody> 
 
     <tr> 
 
      <td> <strong>Total Balance</strong> </td> 
 
      <td id='f_total_balance'> 45.67 </td> 
 
     </tr> 
 
     <tr> 
 
      <td> <strong>Credit</strong> </td> 
 
      <td id='f_content_credit'> 10.20 </td> 
 
     </tr> 
 
     <tr> 
 
      <td> <strong>TIP BOX</strong></td> 
 
      <td> 
 
       <select name="tip" class='form-control'> 
 
        <option value="0" selected>0 Kredi</option> 
 
        <option value="0.10">0.1 credit</option> 
 
        <option value="0.20">0.2 credit | not working</option> 
 
        <option value="0.30">0.3 credit</option> 
 
        <option value="0.80">0.8 credit</option> 
 
        <option value="1.20">1.2 credit | not working</option> 
 
       </select> 
 
      </td> 
 
     </tr> 
 
     <tr> 
 
      <td> <strong>After Confirm Calculation</strong> <br><small>Total Balance - Credit - Tip</small> </td> 
 
      <td id='f_after_confirm'> 35.47 </td> 
 
     </tr> 
 
    </tbody> 
 
</table>

+2

这可以帮助你http://stackoverflow.com/questions/588004/is-floating-point-math-broken – Alexis

+0

欢迎来到世界d浮点数学!请注意,'parseFloat()'是本地JS方法。它与jQuery完全没有关系 –

+0

@Alexis我几乎想要关闭它作为一个骗局 –

回答

1

尝试这样,使用toFixed()小数点。

$("select[name='tip']").on('change',function(){ 
 
    var thiz = $(this); 
 
    var content_credit = parseFloat($("#f_content_credit").text()).toFixed(2); 
 
    var total_balance = parseFloat($("#f_total_balance").text()).toFixed(2); 
 
    var tip = parseFloat(thiz.val()).toFixed(2); 
 

 
    console.log(total_balance+"-"+content_credit+"-"+tip); 
 
    
 
    $("#f_after_confirm").text(parseFloat(total_balance-content_credit-tip).toFixed(2)); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<table class="table table-bordered table-hover"> 
 
    <tbody> 
 
     <tr> 
 
      <td> <strong>Total Balance</strong> </td> 
 
      <td id='f_total_balance'> 45.67 </td> 
 
     </tr> 
 
     <tr> 
 
      <td> <strong>Credit</strong> </td> 
 
      <td id='f_content_credit'> 10.20 </td> 
 
     </tr> 
 
     <tr> 
 
      <td> <strong>TIP BOX</strong></td> 
 
      <td> 
 
       <select name="tip" class='form-control'> 
 
        <option value="0" selected>0 Kredi</option> 
 
        <option value="0.10">0.1 credit</option> 
 
        <option value="0.20">0.2 credit | not working</option> 
 
        <option value="0.30">0.3 credit</option> 
 
        <option value="0.80">0.8 credit</option> 
 
        <option value="1.20">1.2 credit | not working</option> 
 
       </select> 
 
      </td> 
 
     </tr> 
 
     <tr> 
 
      <td> <strong>After Confirm Calculation</strong> <br><small>Total Balance - Credit - Tip</small> </td> 
 
      <td id='f_after_confirm'> 35.47 </td> 
 
     </tr> 
 
    </tbody> 
 
</table>

+3

'0.2'和'1.2'选项仍然显示OP不期望的值。你需要在'#f_after_confirm'的'text()'中放置的值调用'toFixed()' –

+0

@RoryMcCrossan是的同意你感谢你的注意,现在改变了。 – Bharat

1

问题是由于浮点计算不一致。您可以在评论here中提到的链接@Alexis上阅读更多内容。

要解决您的实际问题,您可以将最终求和值调用toFixed()以将其格式化为所需的小数位数。试试这个:

$("select[name='tip']").on('change', function() { 
 
    var thiz = $(this); 
 
    var content_credit = parseFloat($("#f_content_credit").text()).toFixed(2); 
 
    var total_balance = parseFloat($("#f_total_balance").text()).toFixed(2); 
 
    var tip = parseFloat(thiz.val()).toFixed(2); 
 
    var final = (total_balance - content_credit - tip).toFixed(2); 
 

 
    console.log(final); 
 
    $("#f_after_confirm").text(final); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<table class="table table-bordered table-hover"> 
 
    <tbody> 
 
    <tr> 
 
     <td> <strong>Total Balance</strong> 
 
     </td> 
 
     <td id='f_total_balance'>45.67</td> 
 
    </tr> 
 
    <tr> 
 
     <td> <strong>Credit</strong> 
 
     </td> 
 
     <td id='f_content_credit'>10.20</td> 
 
    </tr> 
 
    <tr> 
 
     <td> <strong>TIP BOX</strong> 
 
     </td> 
 
     <td> 
 
     <select name="tip" class='form-control'> 
 
      <option value="0" selected>0 Kredi</option> 
 
      <option value="0.10">0.1 credit</option> 
 
      <option value="0.20">0.2 credit | works now</option> 
 
      <option value="0.30">0.3 credit</option> 
 
      <option value="0.80">0.8 credit</option> 
 
      <option value="1.20">1.2 credit | works now</option> 
 
     </select> 
 
     </td> 
 
    </tr> 
 
    <tr> 
 
     <td> <strong>After Confirm Calculation</strong> 
 
     <br><small>Total Balance - Credit - Tip</small> 
 
     </td> 
 
     <td id='f_after_confirm'>35.47</td> 
 
    </tr> 
 
    </tbody> 
 
</table>

1

Math.round(100 * tot)/100;它的使用将会固定.12数字

$("select[name='tip']").on('change',function(){ 
 
    var thiz = $(this); 
 
    var content_credit = parseFloat($("#f_content_credit").text()); 
 
    var total_balance = parseFloat($("#f_total_balance").text()); 
 
    var tip = parseFloat(thiz.val()); 
 
    var tot = total_balance-content_credit-tip; 
 
var cal = Math.round(100 * tot)/100; 
 
    console.log(cal); 
 
    
 
    $("#f_after_confirm").text(cal); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<table class="table table-bordered table-hover"> 
 
    <tbody> 
 
     <tr> 
 
      <td> <strong>Total Balance</strong> </td> 
 
      <td id='f_total_balance'> 45.67 </td> 
 
     </tr> 
 
     <tr> 
 
      <td> <strong>Credit</strong> </td> 
 
      <td id='f_content_credit'> 10.20 </td> 
 
     </tr> 
 
     <tr> 
 
      <td> <strong>TIP BOX</strong></td> 
 
      <td> 
 
       <select name="tip" class='form-control'> 
 
        <option value="0" selected>0 Kredi</option> 
 
        <option value="0.10">0.1 credit</option> 
 
        <option value="0.20">0.2 credit | not working</option> 
 
        <option value="0.30">0.3 credit</option> 
 
        <option value="0.80">0.8 credit</option> 
 
        <option value="1.20">1.2 credit | not working</option> 
 
       </select> 
 
      </td> 
 
     </tr> 
 
     <tr> 
 
      <td> <strong>After Confirm Calculation</strong> <br><small>Total Balance - Credit - Tip</small> </td> 
 
      <td id='f_after_confirm'> 35.47 </td> 
 
     </tr> 
 
    </tbody> 
 
</table>

相关问题