Avatar of sscotti
sscottiFlag for United States of America

asked on 

JS function or RegExp to convert formatted currency (e.g. Euro and others) into a number with a decimal point

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.
Regular ExpressionsJavaScript

Avatar of undefined
Last Comment
HonorGod
ASKER CERTIFIED SOLUTION
Avatar of tg_wilk
tg_wilk

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of sscotti
sscotti
Flag of United States of America image

ASKER

Haven't thoroughly test, but it works for the EUR case fine.  Sweet.
Avatar of HonorGod
HonorGod
Flag of United States of America image

Well, you could use a very simple regexp to very that the data looks "right", then, substitute a period for the comma, then use parseFloat() to convert the data to a floating point value.  How does this sound?

Something like this perhaps:
  val = '  123,45 ABC';
  if ( val.search( /(\d+),(\d+)/ ) > -1 ) {
    var amount = RegExp.$1 + '.' + RegExp.$2
    var result = parseFloat( amount )
    alert( '"' + amount + '" = "' + result + '"' );
  } else {
    alert( 'nope' )
  }

Open in new window

JavaScript
JavaScript

JavaScript is a dynamic, object-based language commonly used for client-side scripting in web browsers. Recently, server side JavaScript frameworks have also emerged. JavaScript runs on nearly every operating system and in almost every mainstream web browser.

127K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo