Are there any "parse" commands for jquery?

1

I get a string type: $ 10.00, I wanted to change it to 10.00 so I can calculate it, I tried the code below, but it did not work.

   for (let i=0; i<numColunas; i++){
     valor = (colunas[i].textContent).parse("R$", "");
     alert(valor);    
  }
    
asked by anonymous 31.01.2018 / 17:33

3 answers

4

Use regular expression to remove everything other than numbers and virgula

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

Swap% of% by% with% and transform the value to float, so you can do calculations with it

teste = teste.replace(',', '.');
teste = parseFloat(teste);
    
31.01.2018 / 18:18
0

The function below is compact and solves this problem:

function floatvalue($val){
            $val = str_replace(",",".",$val);
            $val = preg_replace('/\.(?=.*\.)/', '', $val);
            return floatval($val);
}

$string = "12,34";
$number = floatvalue($string);

$r = 40 + 7.5 + $number;

echo $r;

Original publication in English - credit for the solution to "user3690160"

    
31.01.2018 / 17:56
0

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

  

You must also delete all points in the number before   replace the comma, because if the number has a thousand separator,   will give problem (eg. R$10.000,00 will be 10.000.00 ).

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

In your case, it would look like:

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));
    
31.01.2018 / 18:50