I have a need to convert formatted currency, mostly Euro but would apply to other currencies into a purely number format with a decimal point.
e.g. In the case at hand the price is in the URL as 4%2C99%26nbsp%3BEUR, which decoded is 54,99 EUR. I want a function to convert this to 54.99. Similar currencies are:
522,71 SEK
445,79 NOK
etc.
I have an existing function but it doesn't work for currencies that are formatted this way:
function stripCurrency(FormattedPriceWithCurrency) {
var MainNumberExpression = ((NumbersBetweenCommas > 0) && (CommaSeparatorSymbol != '')) ? '\\d{1,' + NumbersBetweenCommas + '}(\\' + CommaSeparatorSymbol + '?\\d{' + NumbersBetweenCommas + '})*' : '\\d+';
var DecimalExpression = (DecimalPointSymbol != '') ? '\\' + DecimalPointSymbol + '?\\d*' : '';
var CurrencyRE = new RegExp('^(\\D*)(' + MainNumberExpression + DecimalExpression + ')(\\D*)$');
var PriceChunk = CurrencyRE.exec(FormattedPriceWithCurrency);
if (PriceChunk == null) return 0;
else {
CurrencyPrefix = PriceChunk[1];
CurrencySuffix = PriceChunk[PriceChunk.length-1];
if (CommaSeparatorSymbol != '') {
var CommaRE = new RegExp('\\' + CommaSeparatorSymbol, 'g');
PriceChunk[2] = PriceChunk[2].replace(CommaRE, '');
}
if ((DecimalPointSymbol != '') && (DecimalPointSymbol != '.')) {
var DecimalRE = new RegExp('\\' + DecimalPointSymbol);
PriceChunk[2] = PriceChunk[2].replace(DecimalRE, '.');
}
return parseFloat(PriceChunk[2]);
}
}
Thanks.
ASKER