sum of values with ng-repeat

Asked

Viewed 1,538 times

0

Good night

I’m having a problem adding up 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’m trying to add the 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 like this 03500.00500.00

  • And if you remove double quotes from the commission value attribute?

2 answers

2


Convert your String (report.VALOR_COMISSAO) to Float, once its 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;
}

1

Remove double quotes from the commission value attribute, did it work? Angularjs, as well as other frameworks and Javascript, recognize double quotes as a String and not a numerical value for performing mathematical operations.

Or use parseFloat if the value is always received as String and double quotes.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.