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()

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:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
	<mx:Script>
		<![CDATA[
			import com.adobe.utils.NumberFormatter;
		]]>
	</mx:Script>

	<mx:VBox width="100%" height="100%">
		<mx:HBox>
			<mx:Label text="Number to be formatted" />
			<mx:TextInput id="number" text="9"/>
		</mx:HBox>

		<mx:Text text="addLeadingZero:{NumberFormatter.addLeadingZero(Number(number.text))}"/>

	</mx:VBox>
</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 Follow us Follow us
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