Do you have any "parse" commands for jquery?

Asked

Viewed 73 times

1

I get a string like : R$10,00, I wanted to change it to 10.00 to be able to calculate, I tried the code below, but it didn’t work.

   for (let i=0; i<numColunas; i++){
     valor = (colunas[i].textContent).parse("R$", "");
     alert(valor);    
  }

3 answers

4


Use regular expression to remove anything other than numbers and virgula

var teste = 'R$10,00';
teste = teste.replace(/[^0-9\,]/g, '');

Change vírgula for ponto and turn the value to float, so you can do calculations with it

teste = teste.replace(',', '.');
teste = parseFloat(teste);

0

0

You can use the replace with a regex that takes only the numbers and comma.

It is also necessary to exclude all points from the number before replace the comma, because if the number has a thousand separator, will give problem (ex. R$10.000,00 will stay 10.000.00).

valor = parseFloat(("R$10.000,55").replace(/[^\d,]/g, "").replace(',','.'));

In your case, I would:

valor = parseFloat((colunas[i].textContent).replace(/[^\d,]/g, "").replace(',','.'));

Example:

valor = parseFloat(("R$10.000,55").replace(/[^\d,]/g, "").replace(',','.'));
console.log((valor+10.99).toFixed(2));

Browser other questions tagged

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