gpt4 book ai didi

javascript - parseFloat 没有给出准确的值

转载 作者:行者123 更新时间:2023-11-29 19:09:06 26 4
gpt4 key购买 nike

我正在从事一个需要“提示框”计算系统的项目。正如您在代码片段中看到的那样,它没有像我预期的那样工作。我该如何解决这个问题?

$("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>

最佳答案

像这样尝试,使用 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>

关于javascript - parseFloat 没有给出准确的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40503309/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com