There is another option, no regex.
First you need to turn the string into number. Unfortunately parseFloat
only accepts the point as decimal separator, so there’s not much to escape from a replace
to exchange a comma for a period.
Then, it was not clear how many decimal places they can have in the string (it is always two, can there be another amount? ), then we can use a little "trick" (actually nothing else is that the good old math) to leave the number to only 2 decimal places, without rounding. To do this, simply multiply the number by 100, round down and then divide by 100 again - more generally, for a quantity x
of houses, just do these calculations with 10x (but if strings always come with 2 houses, this step is not necessary).
Finally, to format the number, we use toLocaleString
, just choose a locale suitable (which in turn controls some aspects, such as the character used as decimal separator), in addition to the number of decimals to be displayed. Would look like this:
function arredonda(s, maxCasas) {
let n = parseFloat(s.replace(',', '.'));
// deixar o número com apenas 'maxCasas' casas decimais, sem arredondar
let fator = Math.pow(10, maxCasas);
n = Math.floor(n * fator) / fator;
let opcoes = {
minimumFractionDigits: 0,
maximumFractionDigits: maxCasas,
useGrouping: false
};
return n.toLocaleString('pt-BR', opcoes );
}
for (const n of [ '2,00', '100,50', '3', '12,347', '12345,6789' ])
console.log(`${n} => ${arredonda(n, 2)}`);
The exit is:
2,00 => 2
100,50 => 100,5
3 => 3
12,347 => 12,34
12345,6789 => 12345,67
The option useGrouping: false
(described here) causes thousands separator not to be used (its value default is true
, then if it is omitted, the last number would be displayed as 12.345,67
).
I used the locale pt-BR
(Brazilian Portuguese), which uses the comma as decimal separator. Thus, the formatting is already done with the correct character (if you use another locale, as an example en-US
(American English), will be used the point instead of the comma). And the other options control the number of decimals to be displayed (using minimum zero and maximum maxCasas
, decimals are not displayed when the number does not have the decimal part, for example).
As already said, if all strings always have 2 decimal places, no rounding is necessary, just call toLocaleString
shortly after the parseFloat
.
If you are working with monetary values, it is interesting to read here.
And if you just want to compare the strings "100.50" and "100.5" based on their numerical values, you didn’t even need to generate the final string, just turn both into numbers (doing the parseFloat
with replace
) and then compare them.
new Number("100.50")
orparseFloat('100.50')
or"100.50".replace(/(?:,?0+)$/g, '')
– Valdeir Psr
Why are the values comma? Do they possibly not start with dot? Something like 100.5 ? Is the way you approached not trying to solve a problem that maybe shouldn’t even exist?
– Guilherme Nascimento