0
I have some fields on my page and they have values, I take these values and we are, it turns out that the value goes wrong, as I take them from the DOM and end up coming as string, I convert into number, so instead of posting all 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, IE, the sum seems to be wrong. Note: I am still learning
It’s not adding up wrong, you’re converting wrong. use parseFloat instead of parseint
– JrD
parse int will return you only the integer value, without the decimal.
– Cristiano Bombazar
See the documentation
– NoobSaibot
Wow, it worked, but it returns only the value like this: 7, there is a way to return equal to real?
– Otavio Fagundes
parseInt
, as the name says, transforms the value into INT, that is, an integer value. If it has a decimal value, it must convert tofloat
, with theparseFloat
.– Máttheus Spoo
@Otaviofagundes use the search field of the site, there are already related questions.
– NoobSaibot
@Otaviofagundes would be like this:
valor.toLocaleString("pt-BR", { style: "currency" , currency:"BRL"});
remembering thatvalor
has to be converted first.– NoobSaibot
Gave it right and still brought me the "R$" automatically, I did not know that javascript had this kind of thing
– Otavio Fagundes