topical media & game development

talk show tell print

actionscript-book-WikiEditor-com-example-programmingas3-wikiEditor-CurrencyConverter.ax

actionscript-book-WikiEditor-com-example-programmingas3-wikiEditor-CurrencyConverter.ax [swf] flex


  package //com.example.programmingas3.wikiEditor 
  {
          
          
Displays a round clock face with an hour hand, a minute hand, and a second hand.

  
          public class @ax-actionscript-book-WikiEditor-com-example-programmingas3-wikiEditor-CurrencyConverter {
                   
Converts strings of US dollar values (such as "$9.95") to Euro strings (such as "8.24 €".

  
                  public static function usdToEuro(input:String):String {
                          
                          
A regular expression matching a pattern for a US dollar value. A symbol ($) followed by a number pattern. The number pattern matches a string of one or more characters that are either digits or commas ( [\d,]+) followed by at least one digit (\d+). Note that parentheses are used to capture the number pattern.

  
                          var usdPrice:RegExp = /$([\d,]+.\d+)+/g;
                          
                          
Replaces the matching dollar strings with the Euro equivalent string. The second parameter defines a function, used to define the replacement string.

  
                          return input.replace(usdPrice, usdStrToEuroStr); 
                  }
                  
                  
Called as the second parameter of the replace() method of a String. As such, the call passes the following parameters: - The matching portion of the string, such as "$9.95" - The parenthetical match, such as "9.95" - The index position in the string where the match begins - The complete string This method takes the second parameter (args[1]), converts it to a number, and then converts it to a Euro string, by applying a conversion factor and then appending the € character.

  
                  private static function usdStrToEuroStr(...args):String {
                          var usd:String = args[1];
                          usd = usd.replace(",", "");
                          var exchangeRate:Number = 0.828017;
                          var euro:Number = Number(usd) * exchangeRate;
                          trace(usd, Number(usd), euro);
                          const euroSymbol:String = String.fromCharCode(8364);
                          return euro.toFixed(2) + " " + euroSymbol;
                  }
          }
  }


(C) Æliens 27/08/2009

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.