merch89 merch89 - 3 months ago 10
Javascript Question

Turning JSON object value into decimal number after calculation

I have pulled in some data for my app via an external JSON url which has resulted in the value I require, which is currently a string that is "0.00"

result.data.app_payamount = "0.00"

When I convert my strings into numbers and calculate a value I am only being returned a part number and not a full decimal value used for currency.
enter image description here
enter image description here

How can I edit this code so the payamount displays a full decimal number suitable for currency?

var deferred = $q.defer();
var results = response.data;
var urlStart = 'http://exmaple.com/api';
if (response.config.url.startsWith(urlStart)) {
angular.forEach(results, function(result, key) {
result.data.CardFee = 2.00;
result.data.app_bookingfee = result.data.CardFee;
result.data.app_payamount = +result.data.app_subtotal + +result.data.app_handling + -result.data.app_discount + +result.data.app_adjustment + +result.data.app_bookingfee;
});

Answer

Try to use Number.toFixed() here.

        var deferred = $q.defer();
    var results = response.data;
    var urlStart = 'http://exmaple.com/api';
    if (response.config.url.startsWith(urlStart)) {
        angular.forEach(results, function(result, key) { 
            result.data.CardFee = 2.00;
            result.data.app_bookingfee = result.data.CardFee;
            result.data.app_payamount = +result.data.app_subtotal + +result.data.app_handling + -result.data.app_discount + +result.data.app_adjustment + +result.data.app_bookingfee;
            result.data.app_payamount = result.data.app_payamount.toFixed(2);
        });