Convert JSON object value to decimal after calculation

I pulled some data for my application through an external JSON url, which led to the value I need, which is currently a string that is "0.00"

result.data.app_payamount = "0.00"

When I convert my strings to numbers and calculate the value, only the part number is returned to me, not the full decimal value used for the currency. enter image description here enter image description here

How can I edit this code so that payalount displays the full decimal number suitable for the 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;
            });
+4
source share
5 answers

Try using 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);
        });
0

js :

n = n.toFixed(2);
+1

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"
0
source

Use parseFloatandtoFixed

<!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>
Run code
0
source

Use the following currency directive to apply decimal places.

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

Source: https://habr.com/ru/post/1652330/


All Articles