Currency/Numeric Formatter

The below custom functions might be of use where you don’t want to use currency formatter or numeric Formatter of AS3

 
public function currencyFormat( number:Number ):String {
var words:Array = String( number ).split( “” ).reverse(); 
  var results:Array = new Array();
 var l:int = words.length;
  for ( var i:int = 0; i < l; i++ ) {
   results.push( words[i] );
   if ( i % 3 == 2 ) {
     results.push( “,” );
   }
   }
   return results.reverse().join( “” );
}

public function prefixZero( number:Number, figure:int ):String {
   var str:String = String( number );
   for ( var i:int = 0; i < figure; i++ ) {
     str = “0” + str;
   }
   return str.substr( str.length – figure, str.length );
}

  1. Note that in the currencyFormat function, if you enter 200000000 you get an extra comma in the first place because the modulus given 0, you should check there are remaining chars with something like this>

    if ( i % 3 == 2 && (i+1)<l)

    Jorge

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: