Sum between the value of inputs failed in certain cases

Asked

Viewed 123 times

1

I cannot add the values and put the mask correctly between the inputs.

For example, I am adding the following values to each input:

  • Input from Membership Value: 1.300,44
  • Input 1 of Dependent Values: 1.300,44
  • Input 2 of Dependent Values: 80,80

And in the input of Total Value will be placed the sum of the value of all inputs together.

With this current code, if I put 80,79 in Input 2 of Dependent Values, the value matches, but 80,80 does not hit. I do not know how to solve it.

Falha

Script of Membership Value:

function mascara(o, f) {
    v_obj = o
    v_fun = f
    setTimeout("execmascara()", 1)
}

function execmascara() {
    v_obj.value = v_fun(v_obj.value)
}

function mreais(v) {
    v = v.replace(/\D/g, "") //Remove tudo o que não é dígito
    v = v.replace(/(\d{2})$/, ",$1") //Coloca a virgula
    v = v.replace(/(\d+)(\d{3},\d{2})$/g, "$1.$2") //Coloca o primeiro ponto
    return v
}

Input from Membership Value:

<input type="text" id="txt1" name="adesao" class="form-control calcular" placeholder="R$" onkeypress="mascara(this,mreais)" onkeyup="calcular()">

Script of Total Value:

<script type="text/javascript">
function calcular() {
    var soma = $('.calcular').get().reduce(function(soma, el) {
        return (parseFloat(el.value.replace(/\./g, "").replace(",", "."), 10) || 0) + soma;
    }, 0);
    document.getElementById('result').value = soma;

    mascara(document.getElementById('result'), mreais);


}
</script>

Input from Total Value:

<input type="text" class="form-control" name="total" id="result" readonly>

Script of Dependent Values:

<script type="text/javascript">
var AddTableRow = function(el) {
    var tbody = $(el).closest('table').find('tbody');
    var row = tbody.find('tr:last').clone();
    var name = row.find('.calcular').attr('name');
    var index = parseInt(name.match(/usuarios\[(\d+)\]\[valordependente\]/)[1], 10) + 1;
    row.find('[name^="usuarios["]').each(function() {
        if (this.name) {
            this.name = this.name.replace(/^usuarios\[\d+\]/, "usuarios[" + index + "]");
        }
    });
    tbody.append(row);
};
</script>

1 answer

2


This is a problem not only of Javascript but of computing in general.

You can use the method .toFixed in its variable soma to normalize this.

function calcular() {
    var soma = $('.calcular').get().reduce(function(soma, el) {
        return (parseFloat(el.value.replace(/\./g, "").replace(",", "."), 10) || 0) + soma;
    }, 0);

    soma = soma.toFixed(2);

    document.getElementById('result').value = soma;
    mascara(document.getElementById('result'), mreais);
}

I didn’t test it, but it should work!

  • Here is a link in English about Floating-Number for those who want to know more about it. http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html

  • Thanks @Rafael

Browser other questions tagged

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