sum of values with ng-repeat

0

Good evening

I'm having a problem adding values with ng-repeat

object 1

COMISSAO_CORRETOR: "5.0"
CPF_CONTRATO: "xxx.xxx.xxx-xx"
NUMERO_CONTRATO: "1234567"
VALOR_BRUTO: "70000.00"
VALOR_COMISSAO: "3500.00"
VALOR_LIQUIDO: "70000.00"

object 2

COMISSAO_CORRETOR: "5.0"
CPF_CONTRATO: "xxx-xxx-xxx-xx"
NUMERO_CONTRATO: "98765442"
VALOR_BRUTO: "10000.00"
VALOR_COMISSAO: "500.00"
VALOR_LIQUIDO: "10000.00"

I want to add VALO_COMISSAO and I'm using the following function

FUNCTION

$scope.GetTotal = function () {
  var total = 0;
    for(var i = 0; i < $scope.relatorios.length; i++){
        var relatorio = $scope.relatorios[i];
        total += (relatorio.VALOR_COMISSAO);
    }
    return total;

}

But the total is being returned 03500.00500.00

    
asked by anonymous 01.09.2015 / 00:06

2 answers

2

Convert your String (Report.VALUE_COMMODATION) to Float, as soon as your attribute is in string it is concatenating the values.

$scope.GetTotal = function () {
  var total = 0;
    for(var i = 0; i < $scope.relatorios.length; i++){
        var relatorio = $scope.relatorios[i];
        total += parseFloat(relatorio.VALOR_COMISSAO);
    }
    return total;
}
    
01.09.2015 / 00:16
1

Remove the double quotation marks from the attribute value attribute, did it work? AngularJS, as well as other frameworks and JavaScript, recognize the double quotation marks as a String and not a numeric value for performing mathematical operations.

Or use parseFloat if the value is always received as String and enclosed in double quotation marks.

    
01.09.2015 / 00:16