0

Hi I have an ajax that gives result of my Balance when a particular is selected. My question is I want it to show with 2 decimal places even the amount is not in decimal form.

For example:

9000 = 9000.00

9000.1 = 9000.10

9000.11 = 9000.11

9000.159 = 9000.16

The form looks like this to give you a view of the result.enter image description here

I have already tried the toFixed that is mostly answered but I cannot seem to make it work here, I have tried 2 codes.

1st code:

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

}

2nd Code:

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

}

Still same results.

Cecatrix
  • 151
  • 3
  • 16
  • Possible duplicate of [Formatting a number with exactly two decimals in JavaScript](https://stackoverflow.com/questions/1726630/formatting-a-number-with-exactly-two-decimals-in-javascript) – milt_on Jun 04 '17 at 13:07
  • I don't know but I'll try. thanks for the link. – Cecatrix Jun 04 '17 at 13:10

2 Answers2

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" />

You need to do this if response.feestudent_amount is string. toFixed needs to be called on Number and then it needs to be set.

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

See the sample code:

Pankaj Shukla
  • 2,657
  • 2
  • 11
  • 18
  • OMG. It worked!!, but I skip the if part because I dont know the right syntax for it to check if string, and the amount is always a number. Thanks for this! – Cecatrix Jun 04 '17 at 13:24
0

.toFixed(2) is in the wrong place.

Instead of:

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

It should be:

$('#subpaymentbalance' + row).val(response.feestudent_amount.toFixed(2));
Danziger
  • 19,628
  • 4
  • 53
  • 83
  • 1
    @Cecatrix Then you clearly have something else wrong in your code. The two options you showed us should be throwing an error, as [`.val()`](http://api.jquery.com/val/#val2) returns a `jQuery` object. so you can't call `.toFixed` on it. `response.feestudent_amount.toFixed(2)` should properly format the number with two decimal places as long as it is actually a `Number`. If the value is coming back as `String`, then you should be getting an error as well. In that case, you should parse it first: `parseFloat(response.feestudent_amount).toFixed(2)` – Danziger Jun 04 '17 at 13:23