总计显示2位小数的金额

时间:2017-06-04 13:05:36

标签: javascript php jquery

嗨我有一个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

}

结果仍然相同。

2 个答案:

答案 0 :(得分:1)

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是字符串,则需要执行此操作。需要在toFixed上调用Number,然后需要设置它。

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

参见示例代码:

答案 1 :(得分:0)

.toFixed(2)位置错误。

而不是:

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

应该是:

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