Sum with javascript returning wrong value

0

I have some fields on my page and they have values, I get those values and somo, it happens that the value goes wrong, how do I get them from the DOM and ends up coming as a string, I convert to a number, so instead of posting everything the code here, comes out something like this:

var valor = parseInt("3.50");
console.log(valor + valor);

The value I expected was 7.00, but it returns me 6, meaning the sum seems to be wrong. Note: I'm still learning

    
asked by anonymous 11.10.2018 / 18:10

1 answer

2
var valor = parseFloat("3.50");
console.log(valor + valor);

You are transforming the string to Integer it will round down to standard, in case you can round up using Math.round() or as you are using real number use parseFloat()

    
11.10.2018 / 18:19