Hey I am trying to write some code to handle the unformatting of EU and US numbers but I can not get a perfect solution.
For example I would like
1.222.222,89 to be 1222222.89
1,222,222.89 to be 1222222.89
I am using AG GRID and I am pasting numbers to the grid which ag grid is storing as strings.
what I have got so far but will not work with numbers with only a single ‘,’ or ‘.’
let unformattedNumber = params.value as string;
unformattedNumber = unformattedNumber.replace("%", "");
unformattedNumber = unformattedNumber.replace(/ /g, "");
const lastCommaIndex = unformattedNumber.lastIndexOf(",");
const lastPeriodIndex = unformattedNumber.lastIndexOf(".");
if (lastCommaIndex > lastPeriodIndex) {
unformattedNumber = unformattedNumber.replace(/./g, "");
unformattedNumber = unformattedNumber.replace(",", ".");
} else if (lastPeriodIndex > lastCommaIndex) {
unformattedNumber = unformattedNumber.replace(/,/g, "");
}
return parseFloat(unformattedNumber);
3
Answers
Seems like you are on the right track. You can try this out:
I hope it works how You want it to be! Used JS a long time ago.
I’m not very good with regex so I used arrays😅. May not be the best practice, but it works:
I tried shortening the
.pop()
process by just usingsplice()
but for some reason, it wasn’t working too well on JSFiddle, here’s the code for that if you want it tho: