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

时间:2016-08-24 12:22:56

标签: javascript angularjs json

我已经通过外部JSON网址为我的应用提取了一些数据,这导致我需要的值,这是一个字符串是" 0.00"

result.data.app_payamount =" 0.00"

当我将字符串转换为数字并计算一个值时,我只返回一个部件号,而不是用于货币的完整十进制值。 enter image description here enter image description here

如何修改此代码,以便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;
            });

5 个答案:

答案 0 :(得分:1)

在js中,您可以指定这样的位数:

n = n.toFixed(2);

答案 1 :(得分:0)

使用number.toFixed(2)

var n = 2;
var nStringInteger = n.toFixed(0);    // "2"
var nString1Decimal = n.toFixed(1);   // "2.0"
var nString2Decimal = n.toFixed(2);   // "2.00"

答案 2 :(得分:0)

尝试在这里使用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);
        });

答案 3 :(得分:0)

使用parseFloattoFixed



<!DOCTYPE html>
<html>
<body>

<p>Click the button to parse different strings.</p>

<button onclick="myFunction()">Try it</button>

<p id="demo"></p>
<p id="demo2"></p>
<script>
function myFunction() {
    var a = parseFloat("10.3265");
    var b = parseFloat("10.00");
    var c = parseFloat("10.33");
    var n = a + b + c;
  document.getElementById("demo").innerHTML = n;
    document.getElementById("demo2").innerHTML = n.toFixed(2);
}
</script>

</body>
</html>
&#13;
&#13;
&#13;

答案 4 :(得分:0)

使用以下货币指令来应用小数位。

app.directive('currency', function ($filter, $locale) {
    return {
        require: 'ngModel',
        scope: {
            min: '=min',
            max: '=max',
            ngRequired: '=ngRequired'
        },
        link: function (scope, element, attrs, ngModel) {

            function clearValue(value) {
                value = String(value);
                var dSeparator = $locale.NUMBER_FORMATS.DECIMAL_SEP;
                var clear = value.match(/[\+\-0-9\.]/g);
                clear = clear ? clear.join("") : 0;
                return clear;
            }

            ngModel.$parsers.push(function (viewValue) {
                cVal = clearValue(viewValue);
                return parseFloat(cVal);
            });

            element.on("blur", function () {
                if (isNaN(ngModel.$modelValue)) {
                    ngModel.$modelValue = 0;
                    element.val($filter('currency')(clearValue(ngModel.$modelValue)));
                }
                else {
                    element.val($filter('currency')(ngModel.$modelValue));
                }
            });


            ngModel.$formatters.unshift(function (value) {
                return $filter('currency')(value);
            });

            scope.$watch(function () {
                return ngModel.$modelValue
            }, function (newValue, oldValue) {
                runValidations(newValue)
            })

            function runValidations(cVal) {
                if (!scope.ngRequired && isNaN(cVal)) {
                    return
                }
                if (scope.min) {
                    var min = parseFloat(scope.min)
                    ngModel.$setValidity('min', cVal >= min)
                }
                if (scope.max) {
                    var max = parseFloat(scope.max)
                    ngModel.$setValidity('max', cVal <= max)
                }
            }
        }
    }
});