Re: [Flashcoders] Custom RegExp problem

2008-07-16 Thread elibol
Hi Adam,

I just wanted to note a few things, to get you on the right track here...

If you're going to extend RegExp, pass vars to the constructor, otherwise
you aren't using the super class at all.

public function CustomRegExp() {
   super("(\d+)(\d{3}(\.|,|$))", "gi");
}

The convert function might then look something like this:

public function convertToCurrency ($val:Number):String {
  var returnString:String = String($val.toFixed(2));
   while (returnString.match(this).length != 0) {
  returnString = (returnString.replace(this, "$1,$2"));
  }
  return returnString;
}


As for the reg ex, since there are no alphas involved, you probably don't
need the i flag set. Since you have g, there isn't a need for the loop. The
function only takes numbers, so you don't need to check for chars ,$. I had
to write a function that would create thousand separators for a number. I
used the following technique, and I came up with this due to my limited
knowledge on regexp, and time constraints. It isn't the most elegant
solution, but it should solve the toughest part of the conversion:

function thousandSeparator(value:Number):String {
  var va:Array = value.toString().split(".");
  va[0] = va[0].replace(new RegExp("(^[0-9]{"+(va[0].length%3 ==
0?3:va[0].length%3)+"}|[0-9]{3})","g"), ",$1").substr(1);
  return va.join(".");
}

You might want to use toFixed(2) in place of toString to get your precision
right, and then it might be a good idea to replace [0-9]'s with d...

Good luck with it,

Melih

blog.computerelibol.com

On Mon, Jul 14, 2008 at 6:49 AM, Adam Jowett <[EMAIL PROTECTED]>
wrote:

> Hey all,
>
> Just wondering if anyone more familiar with RegExp can pick what might be
> causing the convertToCurrency function below to crash the Flash IDE and/or
> browser (no problems if publishing from a straight FlashDevelop project for
> some reason). Below is firstly how I call it, and secondly the class itself:
>
>
> ...
>
> private var _global:Object = GlobalObject.vars;
>
> var currencyFormat:Function = new CustomRegExp().convertToCurrency;
> _global.convertToCurrency = currencyFormat;
>
> ...
>
>
> package utils.CustomRegExp
> {
>   public class CustomRegExp extends RegExp
>   {
>   public function CustomRegExp()
>   {
>   }
> public function convertToCurrency ($val:Number):String
>   {
>   var pattern:RegExp = /(\d+)(\d{3}(\.|,|$))/gi;
>   var returnString:String = String($val.toFixed(2));
> while (returnString.match(pattern).length != 0)
>   {
>   returnString = (returnString.replace(pattern, "$1,$2"));
> }
> return returnString;
>   }
>   }
> }
>
>
> If there is an alternative to the above I would love to see it, but still
> curious why the above would cause problems.
>
> Cheers
> Adam
>
> ___
> Flashcoders mailing list
> Flashcoders@chattyfig.figleaf.com
> http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
>
___
Flashcoders mailing list
Flashcoders@chattyfig.figleaf.com
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders


[Flashcoders] Custom RegExp problem

2008-07-14 Thread Adam Jowett

Hey all,

Just wondering if anyone more familiar with RegExp can pick what might 
be causing the convertToCurrency function below to crash the Flash IDE 
and/or browser (no problems if publishing from a straight FlashDevelop 
project for some reason). Below is firstly how I call it, and secondly 
the class itself:



...

private var _global:Object = GlobalObject.vars;

var currencyFormat:Function = new CustomRegExp().convertToCurrency;
_global.convertToCurrency = currencyFormat;

...


package utils.CustomRegExp
{
   public class CustomRegExp extends RegExp
   {
   public function CustomRegExp()
   {
   }
  
   public function convertToCurrency ($val:Number):String

   {
   var pattern:RegExp = /(\d+)(\d{3}(\.|,|$))/gi;
   var returnString:String = String($val.toFixed(2));
  
   while (returnString.match(pattern).length != 0)

   {
   returnString = (returnString.replace(pattern, "$1,$2"));   
   }
  
   return returnString;

   }
   }
}


If there is an alternative to the above I would love to see it, but 
still curious why the above would cause problems.


Cheers
Adam

___
Flashcoders mailing list
Flashcoders@chattyfig.figleaf.com
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders