Search code examples
htmlcssangularjsmeanjs

How to total sum along with decimal value in angularjs?


I am using MEAN stack in my application with AngularJS as my front-end. How to total sum along with decimal value , actually I got the total sum value but the decimal value is not calculated...My Plunker For Example :- fob value totalsum I got 700, Expecting like 700.57, then convertion rate value total sum I got 124, Expecting like 124.10 , If any one knows the solution help to us thanks....

My controller:-

.filter('sumOfValue', function () {
    return function (data, key) {
        debugger;
        if (angular.isUndefined(data) && angular.isUndefined(key))
            return 0;        
        var sum = 0;

        angular.forEach(data,function(v,k){
            sum = sum + parseInt(v[key]);
        });        
        return sum;
    }
})

My Html:-

<td>{{resultValue | sumOfValue:'invoice_value_fob'}}</td>

     <td>{{resultValue | sumOfValue:'conversion_rate'}}</td>

My Data:-

$scope.sryarndebitnote = [
    {
    "_id": "57ac1b6d82e1c5940ac3c730",
    "user": {
    "_id": "57400c32bd07906c1308e2cf",
    "displayName": "mani selvam"
    },
    "__v": 0,
    "created": "2016-08-11T06:30:05.118Z",
    "shipment_id": "57ac19b982e1c5940ac3c72f",
    "conversion_rate": "62.04",
    "invoice_value_fob_currency": "Rs",
    "invoice_value_fob": "300.231",
    "invoice_quantity_unit": "KG",
    "invoice_quantity": "37",
    "invoice_date": "2016-08-17",
    "supplier_name": "Msd",
    "buyer_name": "Mani selvam .R"
    },

    {
    "_id": "57b5af69df0475401f644b2e",
    "user": {
    "_id": "57400c32bd07906c1308e2cf",
    "displayName": "mani selvam"
    },
    "__v": 0,
    "created": "2016-08-18T12:51:53.671Z",
    "shipment_id": "57b5af5bdf0475401f644b2d",
    "conversion_rate": "62.06",
    "exclusive": true,
    "invoice_value_fob": "400.343",
    "invoice_quantity": "97",
    "supplier_name": "Msd",
    "buyer_name": "Mani selvam .R"
    },]

Then I need decimal value in two digit like 700.57, Not like 700.574 please help us thanks....

I have created Plunker for reference:- Plunker


Solution

  • I think this should solve your problem

    Here

    <input type="number" name="myDecimal" placeholder="Decimal" ng-model="myDecimal" *step="0.01"* />
    

    Saw your plunker. Can you change your sumOfValue filter to following:-

    app.filter('sumOfValue', function () {
        return function (data, key) {
            debugger;
            if (angular.isUndefined(data) && angular.isUndefined(key))
                return 0;        
            var sum = 0;
    
            angular.forEach(data,function(v,k){
                sum = sum + parseFloat(v[key]);
            });        
            return sum.toFixed(2);
        }
    

    Adding working plunker link here