0

I have 5 input fields that I need to get the total of its SUM.

Created data fields and v-model to the fields and at computed: i'm trying to do the SUM

<input type="text" class="form-control" id="ValorImovelPatrimonio" name="ValorImovelPatrimonio" v-model="ValorImovelPatrimonio" required @keydown="$event.keyCode === 13 ? $event.preventDefault() : false" @blur="pegaTotal">
 var vue = new Vue({
        el: '#app',
        data: {
            checked : false,
            deposito: 1,
            patrimonio_nao: false,
            ValorImovelPatrimonio: null,
            ValorAutosPatrimonio: null,
            ValorOutrosPatrimonio: null,
            ValorAcoesPatrimonio: null,
            ValorInvestimentosPatrimonio: null,
            // total: null
        },
...
computed: {
                total: function(){
                    return this.ValorImovelPatrimonio + this.ValorAutosPatrimonio;
                }
            }

What happens:

ValorAutosPatrimonio:"15.000"
ValorImovelPatrimonio:"1.500.000"
total:"1.500.00015.000"

But I expect:

1.515.000

Anyone, please?

2
  • try explicit conversion float(this.ValorImovelPatrimonio) + float(this.ValorAutosPatrimonio) Commented Feb 18, 2019 at 18:27
  • Note that ValorAutosPatrimonio and ValorImovelPatrimonio are strings. You need to convert them to numbers before doing any arithmetic. Commented Feb 18, 2019 at 18:27

1 Answer 1

2

Try to cast that data to numbers like :

return parseFloat(this.ValorImovelPatrimonio) + parseFloat(this.ValorAutosPatrimonio);
Sign up to request clarification or add additional context in comments.

4 Comments

Works! But the sum is wrong. It gave 16.5 and I expect 1,515.000 (millions)
Hummmm, the problem is the vue-the-mask. The sum was right without it
try return parseFloat(this.ValorImovelPatrimonio.replace(/./g,'')) + parseFloat(this.ValorAutosPatrimonio.replace(/./g,''));
Thanks, @Boussadjra Brahim!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.