as3corelib Tutorial:How to Use NumberFormatter Class in Flex

Finder-128x128 NumberFormatter class contains static utility methods for formatting Numbers. Now, there is a mere method defined. The method addLeadingZero “Formats a number to include a leading zero if it is a single digit between -1 and 10.”  , simple but very useful. :)

I found codes “returnString+=NumberUtil.addLeadingZero(dateToFormat.getDate());” from internet. We can see that this is what we need when we do such things, to format year, month, day, hour, minute, second or others whatever.

Search-256x256 Demo | DownloadDownload Full Project

Screenshot:

Enter arbitrary number to see the result method “addLeadingZero.”

NumberFormatter

Methods:

1. addLeadingZero()

  1. public static function addLeadingZero(n:Number):String

Formats a number to include a leading zero if it is a single digit between -1 and 10.

Parameters:

n:Number The number that will be formatted.

Returns:

String A string with single digits between -1 and 10 padded with a leading zero.

The following is full source code of NumberFormatter Demo.mxml:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
  3.     <mx:Script>
  4.         <![CDATA[
  5.             import com.adobe.utils.NumberFormatter;
  6.         ]]>
  7.     </mx:Script>
  8.    
  9.     <mx:VBox width="100%" height="100%">
  10.         <mx:HBox>
  11.             <mx:Label text="Number to be formatted" />
  12.             <mx:TextInput id="number" text="9"/>
  13.         </mx:HBox>
  14.        
  15.         <mx:Text text="addLeadingZero:{NumberFormatter.addLeadingZero(Number(number.text))}"/>
  16.        
  17.     </mx:VBox>
  18. </mx:Application>
Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • DZone
  • Reddit
  • Technorati
  • StumbleUpon
  • Twitter
RSS Enjoy this Post? Subscribe to Ntt.cc

RSS Feed   RSS Feed     Email Feed  Email Feed
You can leave a response, or trackback from your own site.

One Response to “as3corelib Tutorial:How to Use NumberFormatter Class in Flex”

  1. [...] is the summary of the Package com.adobe.utils, include ArrayUtil, DirectoryUtil, DateUtil, IntUtil, NumberFormatter, StringUtil and XMLUtil [...]

Leave a Reply