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);
}
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);
}
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);
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"
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 be10.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));