The problem lies in its function getMoney
, that is not converting the numeric string (in Brazilian format) correctly.
More specifically, the error is due to this regular expression:
/[\D]+/g
She will give match in whole character that is not numeric. This means that you will also remove the decimal separator from the number (,
), which is basically the same thing as multiplying it by 100. Of course, this is not what we want.
To solve, just correct the regular expression.
/[^\d,]+/g
Note that now any character that is not numerical or a comma (which is our decimal separator) will be removed. Then we need to change the comma (,
- which is the decimal separator of the Brazilian currency) for the point (.
- which is the decimal separator that Javascript understands when doing parseFloat
).
We’ll keep:
function getMoney(str) {
return str
.replace(/[^\d,]+/g, '') // Remove caracteres desnecessários.
.replace(',', '.'); // Troca o separador decimal (`,` -> `.`)
}
console.log(getMoney('R$ 123.456,89')); // 123456.89
Now we can calculate the subtraction of the percentage satisfactorily:
function getMoney(str) {
return str.replace(/[^\d,]+/g, '')
.replace(',', '.');
}
const a = 'R$ 320,55';
const b = getMoney(a);
const c = b - b * 0.9;
console.log(c);
// Formatando para o formato de moeda brasileira:
const formatter = new Intl.NumberFormat('pt-BR', {
style: 'currency',
currency: 'BRL'
});
const d = formatter.format(c);
console.log(d); // R$ 32,06
As you can see, it is also not necessary to implement a function like formatReal
at hand, since Javascript already has monetary formatting Apis included, such as Intl.NumberFormat
or Number.prototype.toLocaleString
.
This has more mathematical problem face than programming itself...
– MarceloBoni
hi @Marceloboni ... really. But take a look at what’s happening... updated the post.
– Davi Assumpcao
For the record, the regex could be
/\D+/g
- the brackets are unnecessary in this case.– hkotsubo