javascript – 计算后将JSON对象值转换为十进制数

我通过外部
JSON url为我的应用程序提取了一些数据,这导致了我需要的值,这是一个字符串“0.00”

result.data.app_payamount =“0.00”

当我将我的字符串转换为数字并计算一个值时,我只返回一个部件号而不是用于货币的完整十进制值.
《javascript – 计算后将JSON对象值转换为十进制数》
《javascript – 计算后将JSON对象值转换为十进制数》

如何编辑此代码,以便payamount显示适合货币的完整十进制数?

        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;
            });

最佳答案 尝试在这里使用Number.toFixed().

        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);
        });
点赞