Two numbers do not add, they concatenate

0

You must already be bald to see problems like this. My problem, I think, is a bit different.

I have a method (called transformAccounts) that transforms any type of monetary value to be able to calculate. However, even if I am changing, I can not use these numbers to add up. I made a method to check if the return of the AccountTransform is really numeric, but always true.

Methods:

function transformaParaContas(value){
    if(value.indexOf(".") >= 0){
        value   =   value.replace(/\./g, "");
    }

    value   =   value.replace(",", ".");
    value   =   Number(value);
    value   =   value.toFixed(2);
    return value;
}

// metodo para verificar se é realmente um número 
function isNumber(numero){ 
    return !isNaN(numero);
}

// disparo
$("input[name='txt_valNaoProcedente']").on("keypress", function(){
    var valProc, valNaoProc, total;
    valProc     =   $("input[name='txt_valProcedente']").val();
    valNaoProc  =   $("input[name='txt_valNaoProcedente']").val();
    if(valProc != "" && valNaoProc != ""){
        valProc     =   transformaParaContas(valProc);
        valNaoProc  =   transformaParaContas(valNaoProc);

        console.log(isNumber(valProc));
        console.log(isNumber(valNaoProc));

        total       =   valProc + valNaoProc;

        $("#div_valorDebito").text(valProc + valNaoProc);
    }else{
        $("#div_valorDebito").text("");
    }
});

If I do the following, it works:

$("input[name='txt_valNaoProcedente']").on("keypress", function(){
    var valProc, valNaoProc, total;
    valProc     =   $("input[name='txt_valProcedente']").val();
    valNaoProc  =   $("input[name='txt_valNaoProcedente']").val();
    if(valProc != "" && valNaoProc != ""){
        valProc     =   transformaParaContas(valProc);
        valNaoProc  =   transformaParaContas(valNaoProc);
        valProc     =   Number(valProc);
        valNaoProc  =   Number(valNaoProc);

        console.log(isNumber(valProc));
        console.log(isNumber(valNaoProc));

        total       =   valProc + valNaoProc;

        $("#div_valorDebito").text(valProc + valNaoProc);
    }else{
        $("#div_valorDebito").text("");
    }
});

Would anyone please explain why this happens?

    
asked by anonymous 02.10.2017 / 16:18

1 answer

1

Dear friend, the toFixed(n) function will always return a type string so there is concatenation and not the sum of its values. And that's why when you use Number it works.

For a less dramatic change so to say, consider returning to value its function transformaParaContas(value) already as number, which would be return Number(value);

Another thing, which is just a curiosity, but worth every penny, you have created a total variable that is never being used. Consider removing or improving your code such as:

$("input[name='txt_valNaoProcedente']").on("keypress", function(){
    var valProc, valNaoProc, total = ""; // Valor já iniciado
    valProc     =   $("input[name='txt_valProcedente']").val();
    valNaoProc  =   $("input[name='txt_valNaoProcedente']").val();
    if(valProc != "" && valNaoProc != ""){
        valProc     =   transformaParaContas(valProc);
        valNaoProc  =   transformaParaContas(valNaoProc);
        valProc     =   Number(valProc);
        valNaoProc  =   Number(valNaoProc);

        console.log(isNumber(valProc));
        console.log(isNumber(valNaoProc));

        total       =   valProc + valNaoProc;
    }
    $("#div_valorDebito").text(total);
});
    
02.10.2017 / 16:57