gpt4 book ai didi

javascript - 金额始终显示小数点后 2 位

转载 作者:行者123 更新时间:2023-12-03 04:10:26 25 4
gpt4 key购买 nike

嗨,我有一个ajax,当选择特定项时,它会给出我的余额结果。我的问题是我希望它显示 2 位小数,即使金额不是小数形式。

例如:

9000 = 9000.00

9000.1 = 9000.10

9000.11 = 9000.11

9000.159 = 9000.16

表单如下所示,以便您查看结果。 enter image description here

我已经尝试过大多数已回答的toFixed,但我似乎无法让它在这里工作,我已经尝试了2个代码。

第一个代码:

function specificBalance(row = null)
{

$('#subpaymentamount'+row).val('');
calculateTotalAmount();

var particulars = $('#subparticulars'+row).val();

$.ajax({
url: baseUrl+'/admin/summary/fetchSpecificBalance/'+schoolyearId+'/'+studentId+'/'+particulars,
type: 'post',
dataType: 'json',
success:function(response) {

$('#subpaymentbalance'+row).val(response.feestudent_amount).toFixed(2);

} // /successs
}); // /ajax

}

第二个代码:

function specificBalance(row = null)
{

$('#subpaymentamount'+row).val('');
calculateTotalAmount();

var particulars = $('#subparticulars'+row).val();

$.ajax({
url: baseUrl+'/admin/summary/fetchSpecificBalance/'+schoolyearId+'/'+studentId+'/'+particulars,
type: 'post',
dataType: 'json',
success:function(response) {

parseFloat($('#subpaymentbalance'+row).val(response.feestudent_amount)).toFixed(2);

} // /successs
}); // /ajax

}

结果仍然相同。

最佳答案

var amount = +"100";
$('#subpaymentbalance').val(amount.toFixed(2));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="subpaymentbalance" />

如果 response.feestudent_amount 是字符串,则需要执行此操作。需要在 Number 上调用 toFixed,然后需要对其进行设置。

var amount = +response.feestudent_amount;
$('#subpaymentbalance' + row).val(amount.toFixed(2));

查看示例代码:

关于javascript - 金额始终显示小数点后 2 位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44354403/

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